首先添加jxl的maven依赖:
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
示例代码:
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class JxlTest {
public static void main(String[] args) throws IOException, RowsExceededException, WriteException {
File xlsFile = new File("D:\jxl.xls");
// 创建一个工作簿
WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
// 创建一个工作表
WritableSheet sheet = workbook.createSheet("sheet1", 0);
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 10; col++)
{
// 向工作表中添加数据
sheet.addCell(new Label(col, row, "zifeiy" + row + "," + col));
}
}
workbook.write();
workbook.close();
}
}