生成excel文件 添加架包 就行了
package com.cpsp.util.excel;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.cpsp.model.EmissionPerformance;
/**
*
* @author oliver
*
* @date 2012-5-14
*
*/
public class ExportToExcel {
/**
* @param args
*/
public static void main(String[] args) {
ExportToExcel excelHandle = new ExportToExcel();
List list=new ArrayList();
list.add("sss");
excelHandle.writeExcel( list);
}
/**
* 写入Excel
*
*/
public static void writeExcel(List<EmissionPerformance> list) {
try {
// 写入到那个Excel文件 如:c:\\hello.xls,或者hello.xls(这个是在项目的根目录下)
WritableWorkbook wwb = Workbook.createWorkbook(new File("D:\\hello.xls"));
// 创建Excel工作表 指定名称和位置
WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
// 设置表格的列宽度
ws.setColumnView(0, 5);// 第一列宽14
ws.setColumnView(1, 30);
ws.setColumnView(2, 20);
ws.setColumnView(3, 10);
ws.setColumnView(4, 10);
ws.setColumnView(5, 30);
ws.setColumnView(6, 10);
ws.setColumnView(7, 10);
ws.setColumnView(8, 20);
ws.setColumnView(9, 10);
ws.setColumnView(10, 10);
ws.setColumnView(11, 35);
ws.setColumnView(12, 10);
ws.setColumnView(13, 10);
ws.setColumnView(14, 10);
ws.setColumnView(15, 10);
ws.setColumnView(16, 10);
// **************往工作表中添加数据*****************
// 定义字体格式:字体为:微软雅黑,24号子,加粗
WritableFont titleFont = new WritableFont(WritableFont.createFont("微软雅黑"), 24, WritableFont.NO_BOLD);
WritableFont contentFont = new WritableFont(WritableFont.createFont("楷体 _GB2312"), 12, WritableFont.NO_BOLD);
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
WritableCellFormat contentFormat = new WritableCellFormat(contentFont);
WritableCellFormat contentFormat2 = new WritableCellFormat(contentFont);
contentFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
// 设置格式居中对齐
titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
contentFormat.setAlignment(jxl.format.Alignment.CENTRE);
// ***************将定义好的单元格添加到工作表中*****************
ws.mergeCells(0, 0, 16, 0);// 合并单元格A-G共7列
ws.addCell(new Label(0, 0, "表2-1 2010年重点企业清洁生产绩效表[1](主要污染物削减情况)", titleFormat));
ws.mergeCells(0, 1, 0, 2);// 合并垮了2列。
ws.addCell(new Label(0, 1, "序号", contentFormat));// 合并垮了3列。
ws.mergeCells(1, 1, 1, 2);// 合并垮了2列。
ws.addCell(new Label(1, 1, "企业名称", contentFormat));// 合并垮了3列。
ws.mergeCells(2, 1, 2, 2);// 合并垮了2列。
ws.addCell(new Label(2, 1, "所属行业[2]", contentFormat));
ws.addCell(new Label(3, 1, "废水", contentFormat));
ws.addCell(new Label(3, 2, "万吨", contentFormat));
ws.addCell(new Label(4, 1, "COD", contentFormat));
ws.addCell(new Label(4, 2, "吨", contentFormat));
ws.addCell(new Label(5, 1, "总量核查确认COD减排量[3]", contentFormat));
ws.addCell(new Label(5, 2, "吨", contentFormat));
ws.addCell(new Label(6, 1, "氨氮", contentFormat));
ws.addCell(new Label(6, 2, "吨", contentFormat));
ws.addCell(new Label(7, 1, "总磷", contentFormat));
ws.addCell(new Label(7, 2, "吨", contentFormat));
ws.addCell(new Label(8, 1, "第一类污染物[4]", contentFormat));
ws.addCell(new Label(8, 2, "吨", contentFormat));
ws.addCell(new Label(9, 1, "废气", contentFormat));
ws.addCell(new Label(9, 2, "万标m3", contentFormat));
ws.addCell(new Label(10, 1, "SO2", contentFormat));
ws.addCell(new Label(10, 2, "吨", contentFormat));
ws.addCell(new Label(11, 1, "总量核查确认SO2减排量[3]", contentFormat));
ws.addCell(new Label(11, 2, "吨", contentFormat));
ws.addCell(new Label(12, 1, "粉尘", contentFormat));
ws.addCell(new Label(12, 2, "吨", contentFormat));
ws.addCell(new Label(13, 1, "烟尘", contentFormat));
ws.addCell(new Label(13, 2, "吨", contentFormat));
ws.addCell(new Label(14, 1, "一般固废", contentFormat));
ws.addCell(new Label(14, 2, "吨", contentFormat));
ws.addCell(new Label(15, 1, "危险废物", contentFormat));
ws.addCell(new Label(15, 2, "吨", contentFormat));
ws.mergeCells(16, 1, 16, 2);// 合并垮了2列。
ws.addCell(new Label(16, 1, "其他[5]", contentFormat));
// 向Excel中插入数据
for (int j = 0; j <list.size(); j++) {
EmissionPerformance emissionPerformance=list.get(j);
ws.addCell(new Label(0, j+3, j+1+"", contentFormat));
ws.addCell(new Label(1, j+3, ""+emissionPerformance.getCpEnterprise().getCpEnterpriseName(), contentFormat));
ws.addCell(new Label(2, j+3, "" + emissionPerformance.getCpEnterprise().getIndustryClass().getName(), contentFormat));
ws.addCell(new Label(3, j+3, "" + emissionPerformance.getWasteWater(), contentFormat));
ws.addCell(new Label(4, j+3, "" + emissionPerformance.getCod(), contentFormat));
ws.addCell(new Label(5, j+3, "" +emissionPerformance.getReduceCod(), contentFormat));
ws.addCell(new Label(6, j+3, "" + emissionPerformance.getAmmoniaNitrogen(), contentFormat));
ws.addCell(new Label(7, j+3, ""+emissionPerformance.getPhosphorus(), contentFormat));
ws.addCell(new Label(8, j+3, "" +emissionPerformance.getFirstPollutants(), contentFormat));
ws.addCell(new Label(9, j+3, "" + emissionPerformance.getWasteGas(), contentFormat));
ws.addCell(new Label(10, j+3, "" + emissionPerformance.getSo2(), contentFormat));
ws.addCell(new Label(11, j+3, "" + emissionPerformance.getReduceSo2(), contentFormat));
ws.addCell(new Label(12, j+3, "" + emissionPerformance.getDust(), contentFormat));
ws.addCell(new Label(13, j+3, "" + emissionPerformance.getSmoke(), contentFormat));
ws.addCell(new Label(14, j+3, "" +emissionPerformance.getGeneralSolidWaste(), contentFormat));
ws.addCell(new Label(15, j+3, "" + emissionPerformance.getHazardousWaste(), contentFormat));
ws.addCell(new Label(16, j+3, "" + emissionPerformance.getOther(), contentFormat));
}
// 写入工作表完毕,关闭流
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}