Java使用jxl对Excel进行写操作
程序员文章站
2024-02-24 13:07:52
...
前提条件:引入jxl.jar包
package com.neusoft.excel;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
* jxl写Excel
*
* @author jianggujin
*
*/
public class WriteExcel {
public static void main(String[] args) throws IOException, WriteException, BiffException {
File xlsFile = new File("jxl.xls");
Workbook book = Workbook.getWorkbook(xlsFile);
// 创建一个工作簿
WritableWorkbook workbook = Workbook.createWorkbook(xlsFile, book);
// 创建一个工作表
WritableSheet sheet = workbook.getSheet(0);
int rows = sheet.getRows() - 1;
System.out.println(rows);
for (int row = rows; row < 20; row++) {
for (int col = 0; col < 10; col++) {
// 向工作表中添加数据
sheet.addCell(new Label(col, row, "datsssa" + row + col));
}
}
workbook.write();
workbook.close();
}
}
其中,以下方式创建的Excel为追加模式
File xlsFile = new File("demo.xls");
Workbook book = Workbook.getWorkbook(xlsFile);
// 创建一个工作簿
WritableWorkbook workbook = Workbook.createWorkbook(xlsFile, book);
另外,合并单元格的方法如下
sheet.mergeCells(col1, row1, col2, row2); // 合并单元格
// 其中col1,col2代表列,row1,row2代表行;行列均以0开始
// 从(col1,row2)开始合并到(col2,row2)结束
上一篇: Java輸入数字反转輸出改進版
下一篇: Excel工具类-导出
推荐阅读