1、poi,jxl
2、导入excel的日志,生成text类型日志下载后会直接打开,可能会乱码(浏览器默认编码不一样),换成生成word类型日志下载解决
File file = new File(logFilename);
byte[] b= ("<html>"+logs.toString()+"</html>").getBytes("gbk");
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem fs = new POIFSFileSystem();
fs.createDocument(bais, "WordDocument");
FileOutputStream ostream = new FileOutputStream(file);
fs.writeFilesystem(ostream);
ostream.close();
bais.close();
3、spring mvc excel导出
@RequestMapping(value="exportBindCode",method=RequestMethod.GET)
public ModelAndView exportBindCode(ModelMap model,DeviceBindCode deviceBindCode){
ModelAndView mav = new ModelAndView();
try{
String device_version = deviceBindCode.getDevice_version();
Integer recordTotal = deviceBindCodeService.queryDeviceBindCodeCount(deviceBindCode);
List<DeviceBindCode> deviceBindCodeList = new ArrayList<DeviceBindCode>();
if(recordTotal != null && recordTotal >=0){
deviceBindCodeList =deviceBindCodeService.queryDeviceBindCodeList(deviceBindCode);
}
model.put("deviceBindCodeList", deviceBindCodeList);
model.put("device_version", device_version);
DeviceBindCodeExcelView excelView = new DeviceBindCodeExcelView();
return new ModelAndView(excelView,model);
}catch(Exception ex){
logger.error(ex.getMessage(),ex);
}
return mav;
}
public class DeviceBindCodeExcelView extends AbstractExcelView {
@Override
protected void buildExcelDocument(Map<String, Object> map, HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String device_version = (String)map.get("device_version");
List<DeviceBindCode> deviceBindCodeList = (List<DeviceBindCode>)map.get("deviceBindCodeList");
---------excel操作,写入excel文件
HSSFSheet sheet = workbook.createSheet("sheet名");
response.setContentType("application/vnd.ms-excel");
String filename = new String("excel文件名.xls".getBytes("GBK"), "ISO-8859-1");
response.setHeader("Content-disposition", "attachment;filename=" + filename);
OutputStream ouputStream = response.getOutputStream();
workbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
4、struts导出excel
struts.xml配置
<action name="TestAction_*" class="TestAction" method="{1}">
<result type="json" name="success"/>
<result name="export" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">attachment;filename="${filename}.xls"</param>
<param name="bufferSize">1024</param>
</result>
<result name="fresh">/jsp/test.jsp</result>
</action>
Action的处理方法
public String export() {
HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet("sheet名"); ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
out.flush();
filename = new String("excel文件名".getBytes("GBK"), "ISO-8859-1");
byte[] content = out.toByteArray();
out.close();
excelStream=new ByteArrayInputStream(content); return "export";
}
filename ,excelStream为Action属性
private String filename;
private ByteArrayInputStream excelStream