栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习笔记:Excel文件的读写

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习笔记:Excel文件的读写

Excel格式说明

1、Excel有xls、xlsx两种格式,推荐使用xlsx,因为没有行数限制。

2、整个Excel文件是一个Workbook,每个Workbook由多个Sheet组成,一个Sheet有多Row,一个Row有多个Cell。拿一个Excel文件去说明。

ExcelHelpers使用

1、看文档研究ExcelHelpers。它是对poi库的简单封装,更多功能看poi的文档。

2、注意的问题,如果使用公式的话,如果运行期间改变了数值,并且想得到计算后的单元格的值,需要手动调用evaluateAllFormulas()。

3、演示:使用代码读取遍历一个Excel文件。

4、演示:使用代码创建一个Excel文件,填充一些数据,然后保存。

限定符和类型方法和说明
static voidclose(org.apache.poi.ss.usermodel.Workbook wb)

关闭Workbook

static org.apache.poi.ss.usermodel.CellStylecreateCellStyle(org.apache.poi.ss.usermodel.Cell cell)

创建CellStyle对象

static org.apache.poi.xssf.usermodel.XSSFChartcreateChart(org.apache.poi.ss.usermodel.Sheet sheet, int col1, int row1, int col2, int row2)

在sheet上创建一个图表对象,显示到左上角坐标为(col1,row1)、右下角坐标为(col2,row2)这个位置。

static org.apache.poi.xssf.usermodel.XSSFChartcreateChart(org.apache.poi.ss.usermodel.Sheet sheet, int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2)

在sheet上创建一个图表对象,显示到左上角坐标为(col1,row1)、右下角坐标为(col2,row2)这个位置。

static org.apache.poi.hssf.usermodel.HSSFWorkbookcreateXLS()

创建旧的2003格式(*.xls)的Excel文档Workbook对象。

static org.apache.poi.xssf.usermodel.XSSFWorkbookcreateXLSX()

创建新格式(*.xlsx)的Excel文档Workbook对象。

static voidevaluateAllFormulas(org.apache.poi.ss.usermodel.Workbook wb)

重新计算workbook这个表格中所有的公式。

static org.apache.poi.ss.usermodel.CellgetCell(org.apache.poi.ss.usermodel.Sheet sheet, int rowIndex, int colIndex)

得到sheet的第rowIndex行的第colIndex列的单元格。

static DoublegetCellDoublevalue(org.apache.poi.ss.usermodel.Cell cell)

获得cell的Double类型的值,如果值是空的或者不存在这个单元格,则返回null。

static DoublegetCellDoublevalue(org.apache.poi.ss.usermodel.Sheet sheet, int rowIndex, int colIndex)

获得sheet的第rowIndex行的第colIndex列的Double类型的值,如果值是空的或者不存在这个单元格,则返回null。

static IntegergetCellIntValue(org.apache.poi.ss.usermodel.Cell cell)

获得cell的Integer类型的值,如果值是空的或者不存在这个单元格,则返回null。

static IntegergetCellIntValue(org.apache.poi.ss.usermodel.Sheet sheet, int rowIndex, int colIndex)

获得sheet的第rowIndex行的第colIndex列的Integer类型的值,如果值是空的或者不存在这个单元格,则返回null。

static java.time.LocalDateTimegetCellLocalDateTimevalue(org.apache.poi.ss.usermodel.Cell cell)

获得cell的LocalDateTime类型的值,如果值是空的或者不存在这个单元格,则返回null。

static java.time.LocalDateTimegetCellLocalDateTimevalue(org.apache.poi.ss.usermodel.Sheet sheet, int rowIndex, int colIndex)

获得sheet的第rowIndex行的第colIndex列的LocalDateTime类型的值,如果值是空的或者不存在这个单元格,则返回null。

static java.time.LocalDategetCellLocalDatevalue(org.apache.poi.ss.usermodel.Cell cell)

获得cell的LocalDate类型的值,如果值是空的或者不存在这个单元格,则返回null。

static java.time.LocalDategetCellLocalDatevalue(org.apache.poi.ss.usermodel.Sheet sheet, int rowIndex, int colIndex)

获得sheet的第rowIndex行的第colIndex列的LocalDate类型的值,如果值是空的或者不存在这个单元格,则返回null。

static StringgetCellStringValue(org.apache.poi.ss.usermodel.Cell cell)

获得cell的String类型的值,如果值是空的或者不存在这个单元格,则返回null。

static StringgetCellStringValue(org.apache.poi.ss.usermodel.Sheet sheet, int rowIndex, int colIndex)

获得sheet的第rowIndex行的第colIndex列的String类型的值,如果值是空的或者不存在这个单元格,则返回null。

static org.apache.poi.ss.usermodel.WorkbookopenFile(byte[] bytes)

打开Excel文件,文件的内容是bytes,返回文档的Workbook对象。

static org.apache.poi.ss.usermodel.WorkbookopenFile(File file)

打开Excel文件file,返回文档的Workbook对象。

static org.apache.poi.ss.usermodel.WorkbookopenFile(InputStream inStream)

打开inStream代表的Excel文件,返回文档的Workbook对象。

static org.apache.poi.ss.usermodel.WorkbookopenFile(String filename)

打开Excel文件filename,返回文档的Workbook对象。

static voidsaveToFile(org.apache.poi.ss.usermodel.Workbook workbook, File file)

把workbook保存到文件file中。

static voidsaveToFile(org.apache.poi.ss.usermodel.Workbook workbook, String filename)

把workbook保存到文件filename中。

static voidsetCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value)

设置cell的值,根据传入的value类型会自动设置单元格的cellStyle。

static voidsetCellValue(org.apache.poi.ss.usermodel.Sheet sheet, int rowIndex, int colIndex, Object value)

设置sheet这个页的第rowIndex行的第colIndex列的值为value。

static byte[]toByteArray(org.apache.poi.ss.usermodel.Workbook workbook)

把workbook生成为内容的字节数组

 无论在界面上显示的是什么,编程都是从第零行第零列开始数的。

所以的数据类型都是继承与object类型的,因此传入String,int等类型都没事

package Part4;

import com.yzk18.docs.ExcelHelpers;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class 遍历Excel文件1 {
    public static void main(String[] args) {
        Workbook workbook=ExcelHelpers.openFile("d:/temp/工作簿1.xlsx");
        for (int sheetIndex=0;sheetIndex 

遍历到有Null存在,出现了问题,DEbug一下

看它在表格的哪里 

可能在Excel文件中有些空cell中存在数据,调试一下如果遇到null就跳过

又运到问题,sheet2打印不出来。继续找问题,Cannot invoke "org.apache.poi.ss.usermodel.Row.getFirstCellNum()" because "row" is null。它说我行是空。OK,那我就在行中加入,遇到null就跳过。

 OK成功了

 Excel遍历完整代码

package Part4;

import com.yzk18.docs.ExcelHelpers;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class 遍历Excel文件1 {
    public static void main(String[] args) {
        Workbook workbook=ExcelHelpers.openFile("d:/temp/工作簿1.xlsx");
        for (int sheetIndex=0;sheetIndex 

 创建Excel文件

代码

package Part4;

import com.yzk18.docs.ExcelHelpers;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class 创建Excel文件1 {
    public static void main(String[] args) {
        XSSFWorkbook worbook = ExcelHelpers.createXLSX();
        XSSFSheet sheet = worbook.createSheet();
        ExcelHelpers.setCellValue(sheet,0,0,"姓名");
        ExcelHelpers.setCellValue(sheet,0,1,"年龄");
        ExcelHelpers.setCellValue(sheet,0,2,"手机号");

        ExcelHelpers.setCellValue(sheet,1,0,"小明");
        ExcelHelpers.setCellValue(sheet,1,1,18);
        ExcelHelpers.setCellValue(sheet,1,2,"18888888888888");

        ExcelHelpers.setCellValue(sheet,2,0,"小红");
        ExcelHelpers.setCellValue(sheet,2,1,19);
        ExcelHelpers.setCellValue(sheet,2,2,"139999999999999");

        ExcelHelpers.saveToFile(worbook,"d:/temp/1.xlsx");
    }
}

效果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/726290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号