生成Excel文件并写入数据

导入的包是jxl的包


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class WriteExcel {
//btlist标题栏和相关字段
//nrlist内容
//wjm文件名
public static void WriteExcel(List btlist,List> nrlist,String wjm){
try {
int length=btlist.size();//获取字段数目
FileOutputStream fos=new FileOutputStream("C:\\zc.xls");//文件的输出位置
WritableWorkbook workbook=Workbook.createWorkbook(fos);
WritableSheet sheet=workbook.createSheet("First sheet", 0);
//设置一下标题样式
WritableFont bold=new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);
WritableCellFormat btformat=new WritableCellFormat(bold);
//保存标题
for (int i = 0; i < length; i++) {
 Label label=new Label(i, 0, btlist.get(i).getFIELD_SHOW_NAME(),btformat);
 sheet.addCell(label);
}
//保存内容
for (int i = 0; i for (int j = 0; j Label label=new Label(j, i+1, String.valueOf(nrlist.get(i).get(btlist.get(j).getFIELD_NAME())));
 sheet.addCell(label);
}
}
workbook.write();
workbook.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("失败",e);
}
}
public static void main(String[] args) {
}
}


你可能感兴趣的:(java大笔记)