linux poi生成excel demo调试附调用代码

时间:2022-03-29 02:20:18

1.下载poi-3.9-20121203.jar包

2.java code

package com.userpackage;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelDemo {
public static void main(String[] args) {
final HSSFWorkbook wb = new HSSFWorkbook();
for (int i = 0; i < 5; i++) {
final HSSFSheet sheet = wb.createSheet("result" + "_" + i);
HSSFRow row;
for (int rowId = 0; rowId < 10; rowId++) {
row = sheet.createRow(rowId);
HSSFCell cell = null;
for (int colId = 0; colId < 10; colId++) {
cell = row.createCell(colId);
cell.setCellValue("row" + rowId + "col" + colId);
}
}
} final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHH24mmssSSS");
final String formatStr = formatter.format(new Date()); final String fileName = "exceldemo" + formatStr + ".xls";
try {
final FileOutputStream xlsStream = new FileOutputStream(fileName);
wb.write(xlsStream);
xlsStream.flush();
xlsStream.close();
} catch (final Exception e) {
e.printStackTrace();
} }
}

3.调用代码

mc11>java -cp /home/ap/user/webproject/web.war/WEB-INF/lib/*:. com.userpackage.ExcelDemo

说明:/home/ap/user/webproject/web.war/WEB-INF/lib/ 放poi包的目录,多个目录用冒号分隔,最后一个圆点表示当前路径

当前路径:/home/ap/user/webproject/web.war/WEB-INF/classes

ExcelDemo路径:/home/ap/user/webproject/web.war/WEB-INF/classes/com/userpackage/ExcelDemo

生成excel文件路径:/home/ap/user/webproject/web.war/WEB-INF/classes/exceldemo20100805.xls