您的位置:首页 > 其它

Excel的导出与下载

2015-07-21 10:42 507 查看
<span style="font-size:18px;">public class ExportExcelUtil {

public static void createExcel(HttpServletResponse response, List pageDataList, String fileName,String date<span style="white-space:pre">				</span>Type,String orgType){
try {
OutputStream out = response.getOutputStream();
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; fileName=" +</span>
<span style="font-size:18px;"> <span style="white-space:pre">						</span>new String((fileName+".xls").getBytes(), "iso8859-1"));
WritableWorkbook wbook = Workbook.createWorkbook(out); // 建立excel文件
WritableSheet wsheet = wbook.createSheet("导出数据", 0); // sheet名称
WritableCellFormat cellFormatNumber = new WritableCellFormat();
cellFormatNumber.setAlignment(Alignment.RIGHT);

WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 定义格式、字体、粗体、斜体、下划线、颜色
WritableCellFormat wcf = new WritableCellFormat(wf); // title单元格定义
WritableCellFormat wcfc = new WritableCellFormat(); // 一般单元格定义
WritableCellFormat wcfe = new WritableCellFormat(); // 一般单元格定义
wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
wcfc.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式

wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
wcfc.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
wcfe.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);

wsheet.setColumnView(0, 20);//设置列宽
wsheet.setColumnView(1, 10);
wsheet.setColumnView(2, 20);

int rowIndex = 0;
int columnIndex = 0;
if (null != pageDataList) {
//rowIndex++;
columnIndex = 0;
wsheet.setRowView(rowIndex, 500);//设置标题行高
wsheet.addCell(new Label(columnIndex++, rowIndex, fileName,wcf));
if("dept".equals(orgType)){
wsheet.mergeCells(0, rowIndex, "monthly".equals(dateType) ? 5 : 4 , <span style="white-space:pre">						</span>rowIndex);//合并标题所占单元格
}else{
wsheet.mergeCells(0, rowIndex, "dept".equals(dateType) ? 4 : 3 , ro<span style="white-space:pre">					</span>wIndex);//合并标题所占单元格
}
//wsheet.mergeCells(0, rowIndex, "monthly".equals(dateType) ? 5 : 4 , rowIn<span style="white-space:pre">						</span>dex);//合并标题所占单元格
rowIndex++;
columnIndex = 0;
wsheet.setRowView(rowIndex, 380);//设置项目名行高
if("dept".equals("orgType")){
wsheet.addCell(new Label(columnIndex++, rowIndex, "编号",wcf));
wsheet.addCell(new Label(columnIndex++, rowIndex, "报销人",wcf));
wsheet.addCell(new Label(columnIndex++, rowIndex, "报销总额",wcf));
wsheet.addCell(new Label(columnIndex++, rowIndex, "年份",wcf));
if("monthly".equals(dateType)){
wsheet.addCell(new Label(columnIndex++, rowIndex, "月份",wcf));
}
wsheet.addCell(new Label(columnIndex++, rowIndex, "部门",wcf));
}else{
wsheet.addCell(new Label(columnIndex++, rowIndex, "部门编号",wcf));
wsheet.addCell(new Label(columnIndex++, rowIndex, "部门",wcf));
wsheet.addCell(new Label(columnIndex++, rowIndex, "报销总额",wcf));
wsheet.addCell(new Label(columnIndex++, rowIndex, "年份",wcf));
if("monthly".equals(dateType)){
wsheet.addCell(new Label(columnIndex++, rowIndex, "月份",wcf));
}
}
/*wsheet.addCell(new Label(columnIndex++, rowIndex, "编号",wcf));
wsheet.addCell(new Label(columnIndex++, rowIndex, "报销人",wcf));
wsheet.addCell(new Label(columnIndex++, rowIndex, "报销总额",wcf));
wsheet.addCell(new Label(columnIndex++, rowIndex, "年份",wcf));
if("monthly".equals(dateType)){
wsheet.addCell(new Label(columnIndex++, rowIndex, "月份",wcf));
}
wsheet.addCell(new Label(columnIndex++, rowIndex, "部门",wcf));*/
// 开始行循环
for (Object objt : pageDataList) { // 循环列
Object[] array = (Object[])objt;
rowIndex++;
columnIndex = 0;

wsheet.addCell(new Label(columnIndex++, rowIndex, Util.nvl(<span style="white-space:pre">							</span>array[0]),wcfe));
wsheet.addCell(new Label(columnIndex++, rowIndex, Util.nvl(<span style="white-space:pre">							</span>array[1]),wcfe));
wsheet.addCell(new Label(columnIndex++, rowIndex, Util.nvl(<span style="white-space:pre">							</span>array[2]),wcfe));
wsheet.addCell(new Label(columnIndex++, rowIndex, Util.nvl(<span style="white-space:pre">							</span>array[3]),wcfe));
if("dept".equals(orgType)){
wsheet.addCell(new Label(columnIndex++, rowIndex, U<span style="white-space:pre">								</span>til.nvl(array[4]),wcfe));
if("monthly".equals(dateType)){
wsheet.addCell(new Label(columnIndex++, row<span style="white-space:pre">									</span>Index, Util.nvl(array[5]),wcfe));
}
}else{
if("monthly".equals(dateType)){
wsheet.addCell(new Label(columnIndex++, row<span style="white-space:pre">									</span>Index, Util.nvl(array[4]),wcfe));
}
}

}
rowIndex++;
columnIndex = 0;
}
wbook.write();
if (wbook != null) {
wbook.close();
}
out.close();
} catch (Exception e) {
e.printStackTrace();

}

}

}</span>


<!-- struts.xml 中结果参数基本配置  -->

<span style="white-space:pre"> </span><span style="font-size:18px;"><result name="detailExcel" type="stream">
<param name="inputName">inputStream</param>
<param name="contentType">application/ms-excel</param>
<param name="contentDisposition">attachment;filename="报表信息.xls"</param>
<param name="bufferSize">1024</param>
</result></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  操作 excel