Apache POI:在 java 中创建、读取和编辑 Excel 文件
通常,电子表格在财务和会计领域被大量使用,以促进发票的计算和创建、报告的管理等。如果应用程序的任何部分需要此类操作:创建、读取或写入,则可以使用多个 API,其中最强大的是 APACHE.的 API POI 还可以处理 Word 和 PowerPoint 文档,随着时间的推移,用户更有信心。
下载
您可以从以下位置下载 API页面 APACHE POI。下载后,导入以下.jar:在您的项目中:- poi
- poi-ooxml
- poi-ooxml-schemas
- xmlbeans
创作和写入 xls
处理 excel 文件的两个主要类是:
HSSFWorkbook: 对于扩展名 xls.
XSSFWorkbook:Microsoft对于扩展名为 xlsx.
的 Excel 2007 文件,下面的代码创建一个具有不同类型值的数组:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel {
public static void main(String[] args) {
//1.创建一个空文档
XSSFWorkbook wb = new XSSFWorkbook();
//2.创建一个空的 Spreadsheet
Sheet = wb.createSheet(new sheet”);
//3.创建一个行并在其中放置一些东西
Row row = sheet.createRow((short)0);
//4.创建一个新的 Cell
Cell 单元格 = row.createCell(0);
//5.设置值
cell.setCellValue(1.2);
//添加更多不同类型的单元格
/*int*/row.createCell(1).setCellValue(3);
/*char*/row.createCell(2).setCellValue('c');
/*String*/row.createCell(3).setCellValue(字符串”);
/*布尔值*/row.createCell(4).setCellValue(false);
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(nouveauFichier.xlsx”);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我们要插入当前日期和时间,Date 格式创建如下:
//插入单元格 F1
cell = row.createCell((short) 6);
cell.setCellValue(new Date());
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFDataFormat xssfDataFormat = wb.createDataFormat();
//创建日期和时间格式
cellStyle.setDataFormat(xssfDataFormat.getFormat(dd/mm/yyyy h:mm”));
cell.setCellStyle(cellStyle);
文本格式
文本格式包括:字体、大小等。斜体/粗体/下划线、颜色、背景和对齐方式.
我们将应用于单元格的对齐示例 日期:
更改 font
/*创建新字体*/
字体字体 = wb.createFont();
//size: 12px
font.setFontHeightInPoints((short)12);
font.setFontName(新快递”);
font.setItalic(true);
font.setBold(true);
/*创建新样式*/
CellStyle cs = wb.createCellStyle();
cs.setFont(font);
//样式单元格 3(D1)
row.getCell(3).setCellStyle(cs);
/*更改背景颜色*/
XSSFCellStyle csColor=wb.createCellStyle();
csColor.setFillForegroundColor(new XSSFColor(new Color(194, 154, 250))));
csColor.setFillPattern(csCouleur.SOLID_FOREGROUND);
//将样式应用于单元格 3
row.getCell(2).setCellStyle(csColor);
/*更改字体颜色*/
字体 font = wb.createFont();
font.setColor((短)45);
CellStyle csCF = wb.createCellStyle();
csCF.setFont(font);
//将样式应用于单元格 0
row.getCell(0).setCellStyle(csCF);
合并单元格
在此示例中,我们将水平和垂直合并四个单元格并将其居中: B2、C2、B3、C3 和addMergedRegion 它以要合并的单元格范围为参数。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
public class Fusion{
public static void main(String[] args) throws FileNotFoundException {
Workbook wb = new HSSFWorkbook();
工作表 = wb.createSheet(sheet1”);
行行 = sheet.createRow((short) 1);
单元格单元格 = row.createCell((short) 1);
cell.setCellValue(单元格合并测试”);
sheet.addMergedRegion(new CellRangeAddress(
1, //第一行 B2
2, //最后一行 B3
1, //第一列 C2
2 //最后一列 C3
));
/*中心*/
cell.getCellStyle().setAlignment((short)2);
cell.getCellStyle().setVerticalAlignment((short)1);
FileOutputStream fs = null;
try {
fs = new FileOutputStream(testFusion.xlsx”);
wb.write(fs);
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
公式用法
Excel 主要用于计算和使用结果单元格中有时复杂的公式。Apache poi 提供了非常有效的方法来添加和测试单元格及其公式。
以下代码处理 4 个学期平均值的简单计算。公式为 (A2+B2+C2+D2)/4.
以下代码处理 4 个学期平均值的简单计算。公式为 (A2+B2+C2+D2)/4.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
导入org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
导入org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel_formule {
public static void main(String[] args) {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(平均”);
行行 = sheet.createRow((short) 0);
row.createCell(0).setCellValue(Quarter1”);
row.createCell(1).setCellValue(Quarter2”);
row.createCell(2).setCellValue(Quarter3”);
row.createCell(3).setCellValue(Quarter4”);
row.createCell(4).setCellValue(平均”);
行 row1 = sheet.createRow((short) 1);
row1.createCell(0).setCellValue(2);
row1.createCell(1).setCellValue(5);
row1.createCell(2).setCellValue(1);
row1.createCell(3).setCellValue(7);
row1.createCell(4).setCellFormula((A2+B2+C2+D2)/4”);
try {
FileOutputStream out = new FileOutputStream(new File(formuletest.xlsx”));
wb.write(out);
out.close();
System.out.println(excel 文件创建成功”);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
评估和浏览 cells
要显示单元格的值,您需要知道其类型。Apache poi 提供类 FormulaEvaluator.evaluateFormulaCell选中复选框以查看是否有公式。如果是这样,它会计算它并返回 formula.
import java.io.File;输出
import java.io.FileInputStream;
import java.io.IOException;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.FormulaEvaluator;
导入org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
导入org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel_parcourir {
public static void main(String[] args) 抛出 IOException {
FileInputStream file = new FileInputStream(new File(formuletest.xlsx”));
//创建一个引用 xlsx 文件的工作簿实例
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet 表 = wb.getSheetAt(0);
公式评估器 formulaEvaluator =
wb.getCreationHelper().createFormulaEvaluator();
for (行行: sheet) {//浏览行
for (单元格单元格: row) {//浏览列
//评估单元格类型
switch (formulaEvaluator.evaluateInCell(cell).getCellType())
{
box Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + \t\t”);
遗产;
框 Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + \t”);
遗产;
}
}
System.out.println();
}
}
}
Quarter1 Quarter2 Quarter3 Quarter4 平均
2.0 5.0 1.0 7.0 3.75