生成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();
}
}
}
分享到:
相关推荐
java利用jxl生成excel文件,代码直接导入就可以跑(修改自己的生成的ecxel地址)
java JXL导出Excel源码及jfreechart 生成折线图,饼图 java JXL导出Excel源码及jfreechart 生成折线图,饼图 java JXL导出Excel源码及jfreechart 生成折线图,饼图
自己封装的方法,调用 XlHelper.getXl(List<?> voList , Object head , OutputStream ops)方法,直接返回需要的excel,而不需要再去写jxl的方法。第一个参数voList 是excel里的内容,第二个参数head是excel表头。第...
Java生成excel jxl 代码Java生成excel jxl 代码
本例优点运行main方法即可到指定路径看到效果。 jxl创建Excel例子,包含创建合并单元格、设置列/单元格属性(字体、宽、高、边框、背景色、自动换行、水平 垂直对齐方式等)。
例子如下: Object是一个对象实体类,如Studnet.java. List 是一个对象集合,后面的是输出路径. WriteExcel rw=new WriteExcel(); rw.writeExcel(new Object(), List, "f:/a.xls","库存表");
poi jxl 生成EXCEL 报表 POI 用的JAR poi-3.6-20091214.jar jxl 用到的jar jxl-2.6.jar
java通过jxl生成excel实例,读取excel,复制,修改excel文件
利用jxl包结合java反射机制和注释,直接把list生成对应的Excel文件,即只需传入list、对应生成的对象、标题就可以生成excel文件write(String title, List list,Class c),实现一个简单生成excel工具类
java用于生成EXCEL等文件格式的包,直接导入到java工程即可,简单方便,另外还配有jxl api说明文档
java 通过JXL架包,给excel文件添加水印,水印内容可自定义
jxl是对excel强有力的操纵工具,它具有丰富的API可以满足对excel的各种开发需要。
JAVA 解析 Excel 工具 Java 解析、生成 Excel 比较有名的框架有 Apache poi、jxl.zip
NULL 博文链接:https://greatjone.iteye.com/blog/1160522
用jxl生成EXCEL表格,一个非常简单易懂的写法。
Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存&#xff0c;poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题&#xff0c;但POI还是有一些缺陷&#xff0c;...
生成Excel 2000标准格式 支持字体、数字、日期操作 能够修饰单元格属性 支持图像和图表 应该说以上功能已经能够大致满足我们的需要。最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它...
java 生成excel 实例 好用、 肯定能用 非常简短
jxl,Java上用来生成Excel的jar工具包,下载后解压引入工程即可使用
java报表JXL和POI打印设置 设置打印,非常好用 一般文档没有 绝版