java中利用poi团结Struts 2来导出execl表格
第一步写action要领:
public String exportActiveExcel() { String name ="活泼度列表.xls"; try { name = java.net.URLEncoder.encode(name, "UTF-8"); fileName = new String(name.getBytes(), "iso-8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return "success"; }
fileName 为类变量要生成get和set要领,作为文件的名称
第二步把数据写入到一个流里:
public InputStream getInputStream() { // 测试学生 ExportExcel ex = new ExportExcel(); String[] headers = {"学号", "姓名", "年数", "性别", "出生日期"}; JSONArray array=new JSONArray(); JSONArray arr=new JSONArray(); arr.add(10000001); arr.add("张三"); arr.add(20); arr.add(true); arr.add("2013-5-6"); JSONArray arr1=new JSONArray(); arr1.add(10000002); arr1.add("李四"); arr1.add(24); arr1.add(false); arr1.add("2013-5-6"); array.add(arr); array.add(arr1); return ex.exportExcel("活泼度阐明",headers, array); }
用到的类:
public class ExportExcel { @SuppressWarnings("deprecation") public InputStream exportExcel(String title, String[] headers, JSONArray array) { // 声明一个事情薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = workbook.createSheet(title); // 配置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth((short) 30); // 生成一个标题样式 HSSFCellStyle titleStyle = workbook.createCellStyle(); // 配置这些样式 titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 生成一个标题字体 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 titleStyle.setFont(font); // 生成内容样式 HSSFCellStyle contentStyle = workbook.createCellStyle(); contentStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成内容字体 HSSFFont contentFont = workbook.createFont(); contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 contentStyle.setFont(contentFont); // 发生表格标题行 HSSFRow row = sheet.createRow(0); for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(titleStyle); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } //生成内容行 for (int i = 0; i < array.size(); i++) { row = sheet.createRow(i+1); JSONArray arr=(JSONArray) array.get(i); for (int j = 0; j < arr.size(); j++) { HSSFCell cell = row.createCell(j); cell.setCellStyle(contentStyle); HSSFRichTextString richString = new HSSFRichTextString(arr.get(j).toString()); HSSFFont font3 = workbook.createFont(); font3.setColor(HSSFColor.BLUE.index); richString.applyFont(font3); cell.setCellValue(richString); } } //写入输出流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { workbook.write(baos);// 写入 } catch (IOException e) { e.printStackTrace(); } byte[] ba = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(ba); try { baos.close(); } catch (IOException e) { e.printStackTrace(); } return bais; } }
查察本栏目
第三步设置struts.xml文件
<action name="exportActiveExcel" class="activeCountAction" method="exportActiveExcel"> <result name="success" type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="inputName">inputStream</param> <param name="bufferSize">4096</param> </result> </action>
inputName的值要与流的要领一致,生成getXxxxx,否则找不到要领
在页面上直接挪用action要领的路径
<input type="button" value="数据导出" onclick="javascript:window.location.href=’exportActiveExcel’;" />