日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts2 POI 导入导出Excel数据

發布時間:2025/3/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。