此工具类是对于Excel文件的读取、写入及备份。写入的方式为保留Excel文件中原数据并在文件中追加新内容。适用于统计每日数据的需求。
1. 导入依赖
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
2. 对Excel文件的读取
/**
* 读取excel文件
* @return
*/
public static void readExcel(){
InputStream io = null;
Workbook readwb = null;
try {
File file = new File("文件路径");
io = new FileInputStream(file);
//获取工作簿
readwb = Workbook.getWorkbook(io);
//Sheet1: 0 ; Sheet2:1 ; Sheet3:2
Sheet readsheet = readwb.getSheet(0);
//获取表格列数
int rsColumns = readsheet.getColumns();
//获取表格行数
int rsRows = readsheet.getRows();
for (int i = 0; i < rsRows; i++) { //从第一行开始
for(int j = 0; j< rsColumns;j++){ //从第一列开始
Cell cell_name = readsheet.getCell(j, i); //第一列第一行
String contents = cell_name.getContents(); //第一行第一列的值
System.out.print(contents+" ");
}
System.out.println();
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(io!= null){
io.close();
}
if(readwb != null){
readwb.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
3. 对Excel文件的写入(在原文件中追加内容)
/**
* 将数据写入excel文件
* @param listTest 要写入单元格数据
*/
public static void writeExcel(List<String> listTest) {
WritableWorkbook wwb = null;
InputStream io = null;
Workbook wb = null;
try {
File file2 = new File("文件路径");
io = new FileInputStream(file2);
//获取工作簿
wb = Workbook.getWorkbook(io);
//Sheet1: 0 ; Sheet2:1 ; Sheet3:2
Sheet readsheet = wb.getSheet(0);
//创建一个可写入的工作簿
wwb = Workbook.createWorkbook(file2, wb);
// 内容显示格式
WritableCellFormat wcf = new WritableCellFormat();
wcf.setWrap(true); //设置单元格可以换行
wcf.setAlignment(Alignment.CENTRE); //水平居中
wcf.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直居中
wcf.setFont(new WritableFont(WritableFont.TIMES, 11)); // 内容字体 11号
WritableSheet ws = wwb.getSheet(0);
//获取表格行数,从空白行开始写入数据
int k = readsheet.getRows();
//获取昨日的年月日 yyyy-MM-dd 格式
String date = OtherUtils.getYesterday();
for (int i = 0; i < listTest.size(); i++) {
//设置第1列第k行 单元格数据。参数依次为:列、行、数据、内容格式(字体等)
ws.addCell(new Label(0, k, "日期", wcf));
//设置第2列第k行 单元格数据。参数依次为:列、行、数据、内容格式(字体等)
ws.addCell(new Label(1, k, date, wcf));
k++;
}
wwb.write();
log.info("导出excel完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (wwb != null) {
wwb.close();
}
if (io != null) {
io.close();
}
if (wb != null) {
wb.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. 对文件的备份(复制)
/**
* 备份文件
*/
public static void copyFile(){
File sourceFile = new File("原文件路径");
File targetFile = new File("备份文件路径");
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(sourceFile);
out = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
log.info("Excel文件备份完成");
} catch (Exception e) {
log.error("Excel文件备份出错");
} finally {
try {
if(in != null){
in.close();
}
if(out != null){
out.close();
}
}catch (Exception e){
log.error("关闭流出错");
e.printStackTrace();
}
}
}
5. 注意
需要注意的是文件路径及备份文件路径是包括Excel文件的路径。例如上述用到的路径为
//原文件路径
public static final String filePath = "C:\\Users\\yf\\Desktop\\数据统计.xls";
//备份文件路径
public static final String filePath2 = "C:\\Users\\yf\\Desktop\\数据统计-备份.xls";