Struts2 POI 導入導出Excel數據

頁面端:html

<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>java

 

配置struts.xml數組

<action name="exportExcel" class="com.bestbpo.action.ExcelAction">緩存

  <result name="success" type="stream">app

    <param name="contentType">application/vnd.ms-excel</param>框架

    <param name="contentDisposition">attachment;filename=${fileName}</param>post

    <param name="inputName">excelStream</param>ui

    <param name="bufferSize">1024</param>this

  </result>spa

</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;
}
相關文章
相關標籤/搜索