如何使用Apache POI在Microsoft Excel电子表格上执行不同的功能?使用Apache POI读取/写入不同的Microsoft Office文件格式
我试图生成和更新Excel文件从我的Spring MVC应用程序(从数据库数据)..
感谢
如何使用Apache POI在Microsoft Excel电子表格上执行不同的功能?使用Apache POI读取/写入不同的Microsoft Office文件格式
我试图生成和更新Excel文件从我的Spring MVC应用程序(从数据库数据)..
感谢
包括Apache的POI jar文件
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
读取excel文件
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//..
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();
//Get iterator to all cells of current row
Iterator<Cell> cellIterator = row.cellIterator();
我们在上面的代码片段中使用的类,HSSFWorkbook和HSSFSheet作品.xls格式。为了使用.xlsx,请使用XSSFWorkbook和XSSFSheet类。
到HSSF类似,POI有其他的文件格式也不同的前缀:
HSSF(可怕的电子表格格式) - 读取和写入的Microsoft Excel(XLS)格式的文件。
XSSF(XML电子表格格式) - 读取和写入Office Open XML(XLSX)格式文件。
HPSF(可怕属性集格式) - 从Microsoft Office文件读取“文档摘要”格式。
HWPF(可怕的字处理器格式) - 旨在读写Microsoft Word 97中(DOC)格式的文件。
HSLF(可怕的幻灯片布局格式) - Microsoft PowerPoint文件的纯Java实现。
HDGF(图可怕格式) - 为Microsoft Visio二进制文件的初始纯Java实现。
HPBF(Horrible PuBlisher格式) - Microsoft Publisher文件的纯Java实现。
HSMF(可怕的愚蠢的邮件格式) - 一个纯粹的Microsoft Outlook MSG文件的Java实现。
DDF(Dreadful Drawing Format) - 解码Microsoft Office图形格式的包。
创建新的Excel文件
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//..
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FuSsA sheet");
//Create a new row in current sheet
Row row = sheet.createRow(0);
//Create a new cell in current row
Cell cell = row.createCell(0);
//Set value to new value
cell.setCellValue("Slim Shady");
try {
FileOutputStream out =
new FileOutputStream(new File("C:\\new.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
更新现有的Excel文件
try {
FileInputStream file = new FileInputStream(new File("C:\\update.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell = null;
//Update the value of cell
cell = sheet.getRow(1).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
cell = sheet.getRow(2).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
cell = sheet.getRow(3).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
file.close();
FileOutputStream outFile =new FileOutputStream(new File("C:\\update.xls"));
workbook.write(outFile);
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
有关详细信息,添加公式和添加荷兰国际集团的样式到细胞可以检查此链接:Read/Write Excel file in Java using Apache POI
对于任何细胞或加入式操作,就可以使用如下因素:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Calculate Simple Interest");
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("Pricipal Amount (P)");
header.createCell(1).setCellValue("Rate of Interest (r)");
header.createCell(2).setCellValue("Tenure (t)");
header.createCell(3).setCellValue("Interest (P r t)");
Row dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue(14500d);
dataRow.createCell(1).setCellValue(9.25);
dataRow.createCell(2).setCellValue(3d);
dataRow.createCell(3).setCellFormula("A2*B2*C2");
try {
FileOutputStream out =
new FileOutputStream(new File("C:\\formula.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
对于添加样式到细胞,
you can use: cell.setCellStyle(style);
要添加背景到单元格,您可以使用以下内容:
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);