在本文中,我们将讨论如何使用JExcel API(一个简单的库,广泛地用于不涉及高级格式化和复杂公式操作的简单操作)来读写excel文件。
PS已通过JExcel API测试-2.6.12
1.下载JExcel
Maven用户。
pom.xml
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
或直接在此位置下载
2.写入Excel文件
JExcel API示例,向您展示如何创建Excel文件并将数据写入其中。
ExcelWrite.java
package com.mkyong;
import jxl.Workbook;
import jxl.write.*;
import jxl.write.Number;
import java.io.File;
import java.io.IOException;
public class ExcelWrite {
private static final String EXCEL_FILE_LOCATION = "C:\\temp\\MyFirstExcel.xls";
public static void main(String[] args) {
//1. Create an Excel file
WritableWorkbook myFirstWbook = null;
try {
myFirstWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION));
// create an Excel sheet
WritableSheet excelSheet = myFirstWbook.createSheet("Sheet 1", 0);
// add something into the Excel sheet
Label label = new Label(0, 0, "Test Count");
excelSheet.addCell(label);
Number number = new Number(0, 1, 1);
excelSheet.addCell(number);
label = new Label(1, 0, "Result");
excelSheet.addCell(label);
label = new Label(1, 1, "Passed");
excelSheet.addCell(label);
number = new Number(0, 2, 2);
excelSheet.addCell(number);
label = new Label(1, 2, "Passed 2");
excelSheet.addCell(label);
myFirstWbook.write();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
if (myFirstWbook != null) {
try {
myFirstWbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
}
}
输出,将创建一个包含以下内容的Excel文件:
3.读取Excel文件
读取上述Excel文件的示例。
ExcelRead.java
package com.mkyong;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.File;
import java.io.IOException;
public class ExcelRead {
private static final String EXCEL_FILE_LOCATION = "C:\\temp\\MyFirstExcel.xls";
public static void main(String[] args) {
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(new File(EXCEL_FILE_LOCATION));
Sheet sheet = workbook.getSheet(0);
Cell cell1 = sheet.getCell(0, 0);
System.out.print(cell1.getContents() + ":"); // Test Count + :
Cell cell2 = sheet.getCell(0, 1);
System.out.println(cell2.getContents()); // 1
Cell cell3 = sheet.getCell(1, 0);
System.out.print(cell3.getContents() + ":"); // Result + :
Cell cell4 = sheet.getCell(1, 1);
System.out.println(cell4.getContents()); // Passed
System.out.print(cell1.getContents() + ":"); // Test Count + :
cell2 = sheet.getCell(0, 2);
System.out.println(cell2.getContents()); // 2
System.out.print(cell3.getContents() + ":"); // Result + :
cell4 = sheet.getCell(1, 2);
System.out.println(cell4.getContents()); // Passed 2
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} finally {
if (workbook != null) {
workbook.close();
}
}
}
}
上面的代码几乎是可以理解的。 每个单元格或工作表都映射为Java中的一个对象。 在上面的代码中,我们使用了JExcel jar来编写工作表。 通过执行代码,输出如下:
Test Count:1
Result:Passed
Test Count:2
Result:Passed 2
4.将格式添加到Excel文件
通过添加一些格式可以进一步增强示例。 添加格式的简短代码如下所示:
ExcelFormat .java
package com.mkyong;
import jxl.Workbook;
import jxl.write.*;
import jxl.write.Number;
import java.io.File;
import java.io.IOException;
public class ExcelFormat {
private static final String EXCEL_FILE_LOCATION = "C:\\temp\\MyFormattedExcel.xls";
public static void main(String[] args) {
//1. Create an Excel file
WritableWorkbook mySecondWbook = null;
try {
mySecondWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION));
WritableSheet myFirstSheet = mySecondWbook.createSheet("Sheet 1", 0);
WritableCellFormat cFormat = new WritableCellFormat();
WritableFont font = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD);
cFormat.setFont(font);
Label label = new Label(0, 0, "Test Count", cFormat);
myFirstSheet.addCell(label);
Number number = new Number(0, 1, 1);
myFirstSheet.addCell(number);
label = new Label(1, 0, "Result", cFormat);
myFirstSheet.addCell(label);
label = new Label(1, 1, "Passed");
myFirstSheet.addCell(label);
number = new Number(0, 2, 2);
myFirstSheet.addCell(number);
label = new Label(1, 2, "Passed 2");
myFirstSheet.addCell(label);
mySecondWbook.write();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (mySecondWbook != null) {
try {
mySecondWbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
}
}
输出量
该代码将标头格式化为Arial,16px,粗体字体。 还可以探索其他几种受支持的字体和大小。 JExcel有许多附加功能,可用于创建更加精致和格式正确的excel。 本文为您提供了一个开端。 请参考参考资料中的以下链接以更快地进行操作。
参考文献
- JExcel下载链接
- JExcel函数和对象参考
- 完整的JExcel编程指南
- Apache POI库(替代JExcel API)–下载
- Apache POI开发指南和示例
- 什么是在Java中读取Excel工作表的更好的API – JXL或Apache POI
翻译自: https://mkyong.com/java/jexcel-api-reading-and-writing-excel-file-in-java/