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>
相关文章推荐
- 使用Python生成Excel格式的图片
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- excel vba 限制工作表的滚动区域代码
- excel vba 高亮显示当前行代码
- Flex字符串比较 还有Flex字符串操作
- ASP Cookies操作的详细介绍与实例代码
- perl数据库添加、删除、更新、查询操作例子
- 总结的5个C#字符串操作方法分享
- SQL 导入导出Excel数据的语句
- 文本、Excel、Access数据导入SQL Server2000的方法
- C#导出数据到Excel文件的方法
- C#操作XML文件实例汇总
- Vbscript生成Excel报表的常用操作总结
- C#中父窗口和子窗口之间控件互操作实例
- C#操作PowerPoint的方法
- C#实现导入CSV文件到Excel工作簿的方法
- SQL语句 操作全集 学习mssql的朋友一定要看
- C#模拟window操作鼠标的方法
- C#基于NPOI生成具有精确列宽行高的Excel文件的方法
- 总提示[Microsoft][ODBC Excel Driver] 数值字段溢出官方解决方法