Struts2 POI 导入导出Excel数据
頁面端:
<html>
<head>
<title>導入數據</title>
</head>
<body>
<h1>導入excel數據</h1>
<s:form action="import" method="post" enctype="multipart/form-data">
導入Excel文件:<s:file name="excelFile" /> <br />
<s:submit value="導入"></s:submit>
</s:form>
</body>
</html>
?
配置struts.xml
<action name="exportExcel" class="com.bestbpo.action.ExcelAction">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="inputName">excelStream</param>
<param name="bufferSize">1024</param>
</result>
</action>
ExcelAction類:extends ActionSupport
InputStream excelStream;
String fileName;
操作poi
private void exportExcel(OutputStream os) {
Workbook workbook = null;
workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("學生信息");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("學號");
row.createCell(1).setCellValue("姓名");
row.createCell(2).setCellValue("性別");
row.createCell(3).setCellValue("生日");
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
List<Student> list = new StudentService().findAll();
for (int i = 1; i <= list.size(); i++) {
Student stu = list.get(i - 1);
row = sheet.createRow(i);
row.createCell(0).setCellValue(stu.getId());
row.createCell(1).setCellValue(stu.getName());
row.createCell(2).setCellValue(stu.getSex());
Cell cell = row.createCell(3);
cell.setCellValue(stu.getBirthday());
cell.setCellStyle(cellStyle);
}
try {
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
}?
?}
如果用struts2提供的下載方法的話:
poi里的workbook.write(接受的是一個OutputStream);
將這個workbook寫入到一個輸出流
先new一個輸出流ByteArrayOutputStreambaos=newByteArrayOutputStream();
workbook.write(baos);
baos.flush();
由于struts2框架 struts.xml 里面配制的是要傳給struts一個輸入流 然后有struts框架在轉換成一個輸出流給頁面提供下載,所以要將workbook寫進的輸出流轉成輸入流:
byte[] aa=baos.toByteArray();//這句代碼是將輸出流轉成一個byte數組
在new一個inputStream
excelStream=newByteArrayInputStream(aa,0,aa.length);
如果不依靠框架:(推薦)
public class ExportExcelAction implements ServletResponseAware { private String format = "xls"; private HttpServletResponse response; private String fileName; //省略getter setter public void setFormat(String format) { //根據請求的文件的格式設置format的內容是 xls還是xlsx } public String execute() throws Exception { //具體代碼見后面 } /**設置響應頭*/ private void setResponseHeader() { //具體代碼見后面 } /**導出數據*/ private void exportExcel(OutputStream os) { //具體代碼見后面 } } public void setFormat(String format) { this.format = format; if ("xls".equals(format)) { this.fileName = "導出數據.xls"; } if ("xlsx".equals(format)) { this.fileName = "導出數據.xlsx"; } } private void setResponseHeader() { response.setContentType("application/octet-stream;charset=iso-8859-1"); try { response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(this.fileName, "UTF-8")); // 客戶端不緩存 response.addHeader("Pragma", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void exportExcel(OutputStream os) { Workbook workbook = null; if ("xls".equals(format)) { workbook = new HSSFWorkbook(); } if ("xlsx".equals(format)) { workbook = new XSSFWorkbook(); } Sheet sheet = workbook.createSheet("學生信息"); Row row = sheet.createRow(0); row.createCell(0).setCellValue("學號"); row.createCell(1).setCellValue("姓名"); row.createCell(2).setCellValue("性別"); row.createCell(3).setCellValue("生日"); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); List<Student> list = new StudentService().findAll(); for (int i = 1; i <= list.size(); i++) { Student stu = list.get(i - 1); row = sheet.createRow(i); row.createCell(0).setCellValue(stu.getId()); row.createCell(1).setCellValue(stu.getName()); row.createCell(2).setCellValue(stu.getSex()); Cell cell = row.createCell(3); cell.setCellValue(stu.getBirthday()); cell.setCellStyle(cellStyle); } try { workbook.write(os); } catch (IOException e) { e.printStackTrace(); } } public String execute() throws Exception { setResponseHeader(); exportExcel(response.getOutputStream()); response.getOutputStream().flush(); response.getOutputStream().close(); return null; }?
?
轉載于:https://www.cnblogs.com/yuzhengdong/p/3200336.html
總結
以上是生活随笔為你收集整理的Struts2 POI 导入导出Excel数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在安全模式下激活xp
- 下一篇: 研究生如何选定课题方向 如何变学神