Apache poi excel document operation
1. introduce
-
What is poi?
Answer: It is used for the operation of excel. It can operate on collections and maps to generate corresponding excel documents. Do Reporting. -
The corresponding iText is pdf operation
-
Two versions of excel
- 1997~2003 version
- The
difference of the 2003 version : the suffix name is different. A certain w*s is 03. .
Xls can be opened with the old version of excel and the new version of excel software.
The new version of excel files will not be backward compatible.
2. Wheels
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
3. demo Take the nine-nine multiplication table as an example
3.1 Generation of xls
/**
* 给定excel文件名 和 excel文档 生成excel文件
* @param excelFileName
* @param wb
*/
void createExcelFile(String excelFileName, Workbook wb) {
// 生成文件,字节流输出
File file = new File(excelFileName);
OutputStream out = null;
try {
out = new FileOutputStream(file);
try {
wb.write(out);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
/**
* 生成xls的excel文档 97~03 年
*/
public void test2() {
// 生成空白文档
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
// 生成sheet页
HSSFSheet sheet1 = hssfWorkbook.createSheet("sheet page1");
// 行
for (int i = 0; i < 9; i++) {
// 创建行
HSSFRow row = sheet1.createRow(i);
// 列
for (int j = 0; j <= i; j++) {
// 创建单元格
HSSFCell cell = row.createCell(j);
String res = (i + 1) + " * " + (j + 1) + " = " + (i + 1) * (j + 1);
// 设置单元格内容
cell.setCellValue(res);
}
}
createExcelFile("excel1.xls", hssfWorkbook);
}
3.2 Generation of xlsx
Changed api
/**
* 生成xlsx 的文档
*/
@Test
public void test3() {
// 空白文档
XSSFWorkbook xssfSheets = new XSSFWorkbook();
// sheet页1
XSSFSheet sheet1 = xssfSheets.createSheet("sheet1");
// 行
for (int i = 0; i < 9; i++) {
// 创建行
XSSFRow row = sheet1.createRow(i);
// 列
for (int j = 0; j <= i; j++) {
// 创建单元格
XSSFCell cell = row.createCell(j);
String res = (i + 1) + " * " + (j + 1) + " = " + (i + 1) * (j + 1);
// 设置单元格内容
cell.setCellValue(res);
}
}
createExcelFile("excel2.xlsx", xssfSheets);
}
3.3 Read xlsx
/**
* 利用poi读取xlsx文件
*/
@Test
public void test4() {
try {
InputStream in = new FileInputStream(new File("excel2.xlsx"));
// 获得excel文档
try {
XSSFWorkbook xfb = new XSSFWorkbook(in);
// sheet 页
XSSFSheet sheet = xfb.getSheetAt(0);
// 最大行的下标
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i < lastRowNum; i++) {
// 拿到每行
XSSFRow row = sheet.getRow(i);
// 最大单元的行下标
short cellNum = row.getLastCellNum();
// 遍历单元格
for (int j = 0; j < cellNum; j++) {
// 获得单元格
XSSFCell cell = row.getCell(j);
if (cell != null)
System.out.print(cell + " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
In the end, all the wheels have to be changed, and then they can be used.