1. 程式人生 > >struts2匯入、匯出 excel

struts2匯入、匯出 excel

前臺:
//前臺頁面

<form action="/service/wxExportTool/inputExce.action" method="post" enctype="multipart/form-data" id="form2">
                       <input type="file" name="file1" id="file1"/> 
                       <input type="hidden" name="uuid2" id="uuid2" value=""/> 
 </form>

//匯入excel

function queRen_DR(){
    claseArt();
    coverit();
    var form = new FormData(document.getElementById("form2"));
    $.ajax({
        url:"/service/wxExportTool/inputExce.action",
        type:"post",
        data:form,
        processData:false,
        contentType:false,
        async: false,
        success:function
(data){
uncoverit(); var flag = data.flag; if (flag == '0') { $("#file1").val(""); $("#haved_SL").val("0"); alert(data.msg); var res = data.data; var batchid = res.batchid; var
successCount = res.successCount; $("#batchid").val(batchid); $("#successCount").html(successCount); shows(2); }else if(flag=='3'){ var batchid = data.data.batchid; var htm="<a target=\"_blank\" href=\"/service/wxExportTool/outPuterrorExcel.action?batchid="+batchid+"\">匯出失敗資料</a>"; $("#outputFail").html(htm); shows(3); alert(data.msg); } else{ alert(data.msg); return; } }, error:function(e){ alert("匯入出錯!"); uncoverit(); } }); }

//轉換成excel

Workbook rwb = Workbook.getWorkbook(in);
            Sheet rs=rwb.getSheet(0);//或者rwb.getSheet(0)
            int clos=rs.getColumns();//得到所有的列
            int rows=rs.getRows();//得到所有的行
            for (int i = 1; i < rows; i++) {
                ItvBatchNew itv = new ItvBatchNew();
                for (int j = 0; j < clos; j++) {
                    //第一個是列數,第二個是行數
                    //預設最左邊編號也算一列 所以這裡得j++
                    String username=rs.getCell(j++, i).getContents().trim();//使用者名稱
                    itv.setUsername(username);
                    String identNbr=rs.getCell(j++, i).getContents().trim();//身份證號碼
                    itv.setIdentnumber(identNbr);
                    String broadNbr=rs.getCell(j++, i).getContents().trim();//寬頻接入號
                    itv.setBoradnumber(broadNbr);
                    String address=rs.getCell(j++, i).getContents().trim();//安裝地址
                    itv.setItvaddress(address);
                    String userphone=rs.getCell(j++, i).getContents().trim();//聯絡電話
                    itv.setUserphone(userphone);
                    String remark=rs.getCell(j++, i).getContents().trim();//備註
                    itv.setRemark(remark);
                    itv.setCreator(creator);
                    itv.setBatchid(batchid);
                }
                }
//匯出excel
request.setCharacterEncoding("utf-8");
          response.setCharacterEncoding("utf-8");
          response.setContentType("application/vnd.ms-excel");
          OutputStream out = response.getOutputStream();
          //報頭用於提供一個推薦的檔名,並強制瀏覽器顯示儲存對話方塊
          //attachment表示以附件方式下載。如果要在頁面中開啟,則改為 inline
          response.setHeader("Content-Disposition", "attachment; filename="+batchid+new String("失敗".getBytes("gbk"), "iso8859-1")+"excel.xls");
          //建立workbook工作薄
          HSSFWorkbook workbook = new HSSFWorkbook();
          //建立工作表
          HSSFSheet sheet = workbook.createSheet("第一頁");
          //設定單元格樣式
          HSSFCellStyle hssfCellStyle = (HSSFCellStyle) workbook.createCellStyle();
          hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中顯示
          hssfCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//縱向居中
        try {
            //建立行
              Row row = sheet.createRow(0);
              //建立單元格
              Cell cell = row.createCell(0);
             //設定第一行第一格的值
              cell.setCellValue("使用者名稱");
              //設定單元格的文字居中顯示
              cell.setCellStyle(hssfCellStyle);
              Cell cell1 = row.createCell(1);
              cell1.setCellValue("身份證號");
              cell1.setCellStyle(hssfCellStyle);
              Cell cell2 = row.createCell(2);
              cell2.setCellValue("寬頻接入號");
              cell2.setCellStyle(hssfCellStyle);
              Cell cell3 = row.createCell(3);
              cell3.setCellValue("安裝地址");
              cell3.setCellStyle(hssfCellStyle);
              Cell cell4 = row.createCell(4);
              cell4.setCellValue("聯絡電話");
              cell4.setCellStyle(hssfCellStyle);
              Cell cell5 = row.createCell(5);
              cell5.setCellValue("備註");
              cell5.setCellStyle(hssfCellStyle);
              Cell cell6 = row.createCell(6);
              cell6.setCellValue("失敗原因");
              cell6.setCellStyle(hssfCellStyle);


               for (int i = 0; i < list.size(); i++) {
                 //建立行
                   Row rows = sheet.createRow(i+1);
                 //建立單元格
                      Cell mapcell = rows.createCell(0);
                      //設定第一行第一格的值
                      mapcell.setCellValue(list.get(i).get("USERNAME"));
                      //設定單元格的文字居中顯示
                      mapcell.setCellStyle(hssfCellStyle);
                      Cell mapcell1 = rows.createCell(1);
                      mapcell1.setCellValue(list.get(i).get("IDENTNUMBER"));
                      mapcell1.setCellStyle(hssfCellStyle);
                      Cell mapcell2 = rows.createCell(2);
                      mapcell2.setCellValue(list.get(i).get("BROADNUMBER"));
                      mapcell2.setCellStyle(hssfCellStyle);
                      Cell mapcell3 = rows.createCell(3);
                      mapcell3.setCellValue(list.get(i).get("ADDRESS"));
                      mapcell3.setCellStyle(hssfCellStyle);
                      Cell mapcell4 = rows.createCell(4);
                      mapcell4.setCellValue(list.get(i).get("USERPHONE"));
                      mapcell4.setCellStyle(hssfCellStyle);
                      Cell mapcell5 = rows.createCell(5);
                      mapcell5.setCellValue(list.get(i).get("REMARK"));
                      mapcell5.setCellStyle(hssfCellStyle);
                      Cell mapcell6 = rows.createCell(6);
                      mapcell6.setCellValue(list.get(i).get("REASON"));
                      mapcell6.setCellStyle(hssfCellStyle);
               }

               workbook.write(out);
//            System.out.println("資料寫入成功!");
              out.flush();
              out.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }