Struts2匯出Excel步驟及問題彙總(二) 下載

dawn009發表於2014-06-04
該部分內容是在《Struts2匯出Excel步驟及問題彙總(一)》的前提下完成的。

方法/步驟

  1. 1

    Action中處理,包含下載中文檔案亂碼、為空的問題。


    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    import java.util.Date;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.springframework.beans.factory.annotation.Autowired;

    import com.bskcare.ch.base.action.BaseAction;
    import com.bskcare.ch.service.ProductCardService;
    import com.bskcare.ch.util.DateUtils;
    import com.bskcare.ch.vo.ProductCard;
    import com.opensymphony.xwork2.ModelDriven;

    public class ProductCardAction2 extends BaseAction implements
           ModelDriven {

       private static final long serialVersionUID = 6658682414884709427L;
       
       @Autowired
       private ProductCardService productCardService;

       private ProductCard pc;

       private String fileName; // 下載檔名稱
       private InputStream excelFile; // 下載檔案流


       public ProductCard getModel() {
           if (null == pc)
               pc = new ProductCard();
           return pc;
       }

       public String download() throws Exception {
           HSSFWorkbook workbook = xxx(); //這個為呼叫service層返回的HSSFWorkbook物件
           ByteArrayOutputStream output = new ByteArrayOutputStream();
           workbook.write(output);
           byte[] ba = output.toByteArray();
           excelFile = new ByteArrayInputStream(ba);
           output.flush();
           output.close();
           return "excel";
       }

       public String getDownloadFileName() {
           return fileName;
       }

       public ProductCard getPc() {
           return pc;
       }

       public void setPc(ProductCard pc) {
           this.pc = pc;
       }

       /**
        * 返回型別為"中文名字-20130612231234.xls"
        * @return
        */
       public String getFileName() throws Exception{
           String tempName = "中文名字"+"-" 
                   + DateUtils.formatDate(DateUtils.LONG_DATE_PATTERN_PLAIN, new Date())
                   + ".xls";
           
          fileName = new String(tempName.getBytes(), "ISO8859-1");
           System.out.println(fileName);
           return fileName;    
       }

       public void setFileName(String fileName) {
           this.fileName = fileName;
       }

       public InputStream getExcelFile() {
           return excelFile;
       }

       public void setExcelFile(InputStream excelFile) {
           this.excelFile = excelFile;
       }

    }

  2. 2

    struts.xml下載項配置說明


    <!-- 下載匯出excle --&gt
     
       application/vnd.ms-excel,charset=ISO8859-1
       attachment;filename="${fileName}"  
       4096
       excelFile  

    END

注意事項

  1. 1

    請注意文中加粗部分內容

  2. 2

    中文解決辦法彙總:


    程式碼部分:

    String tempName = "中文名字"+"-" 
                   + DateUtils.formatDate(DateUtils.LONG_DATE_PATTERN_PLAIN, new Date())
                   + ".xls";
           
    fileName = new String(tempName.getBytes(), "ISO8859-1");


    配置檔案部分:

    application/vnd.ms-excel,charset=ISO8859-1

----------------------&gt>轉載於:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29119536/viewspace-1175699/,如需轉載,請註明出處,否則將追究法律責任。

相關文章