将读到的excel导出数据库

第一种  直接在代码中导出 无配置

import org.apache.struts2.ServletActionContext;

InputStream is = new FileInputStream(filePath + "DocumentExcel\\ziping.xls");
HSSFWorkbook workbook = new HSSFWorkbook(is);

HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(( "表格").getBytes("gb2312"), "ISO-8859-1")
+ ".xls");
ServletOutputStream outStream = null;
try {
outStream = response.getOutputStream();
workbook.write(outStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
outStream.close();
}

 

第二种进行导出 有配置

ByteArrayOutputStream fileOut = new ByteArrayOutputStream();
try {
wb.write(fileOut);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String fn ="表.xls";
byte[] b = fileOut.toByteArray();
inputStream = new ByteArrayInputStream(b);
this.setFilename(new String(fn.getBytes("GBK"), "ISO8859-1"));
fileOut.close();
return "download";

<!--struts-action--!>

<result name="download" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="bufferSize">4096</param>
</result>

posted @ 2016-03-28 16:36  佳偶天成  阅读(136)  评论(0)    收藏  举报