EXCEL的數據中導入到我自己設定的EXCEL模板中_第1頁
EXCEL的數據中導入到我自己設定的EXCEL模板中_第2頁
EXCEL的數據中導入到我自己設定的EXCEL模板中_第3頁
EXCEL的數據中導入到我自己設定的EXCEL模板中_第4頁
EXCEL的數據中導入到我自己設定的EXCEL模板中_第5頁
已閱讀5頁,還剩5頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、<% page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%page import="java.text.SimpleDateFormat"%> <%page import="org.apache.poi.ss.usermodel.CellStyle

2、"%><%page import="mon.InitApp"%><%page import="com.yinjun.service.ITSBalanceService"%><%page import="com.yinjun.entity.TSBalance"%><%page import="java.util.*"%><%page import="org.apache.poi.ss.us

3、ermodel.Cell"%><%page import="org.apache.poi.ss.usermodel.Row"%><%page import="org.apache.poi.ss.usermodel.Sheet"%><%page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%><%page import="org.apache.poi.ss.usermode

4、l.Workbook"%><%page import="java.io.IOException"%><%page import="java.io.FileInputStream"%> <%page import="java.io.InputStream"%>  <% page import="java.io.File"%><% page impor

5、t="com.yinjun.util.*"%><%    request.setCharacterEncoding("utf-8");    String msg = ""    String fileName = request.getParameter("fileName");    

6、; int index = fileName.lastIndexOf("");    if (index > 0)         fileName = fileName.substring(index + 1);         String

7、 title = new SimpleDateFormat("yyyyMMdd").format(new Date()            + fileName;    response.reset();    response.setCharacterEncoding("utf-8");

8、0;   response.setContentType("application/download");    response.setHeader("Content-Disposition", "attachment; filename="            + new String(title.ge

9、tBytes("utf-8"), "iso8859-1");/設置文件名顯示中文    String startDate = request.getParameter("startDate");    String endDate = request.getParameter("endDate");    String 

10、;meterIds = request.getParameter("meterIds");    String pageSize = request.getParameter("pageSize");    String currentPage = request.getParameter("currentPage");    Strin

11、g statistics = request.getParameter("statistics");    String exportWay = request.getParameter("exportWay");     Map<String, String> map = new HashMap<String, String&g

12、t;();    map.put("startDate", startDate);    map.put("endDate", endDate);    map.put("meterIds", meterIds);    map.put("currentPage", currentPage);  &

13、#160; map.put("pageSize", pageSize);    if ("all".equals(exportWay)         map.put("queryAll", "queryAll");/選擇查詢全部        map.put(&quo

14、t;statistics", statistics);    ITSBalanceService tsbalanceService = (ITSBalanceService) InitApp.context            .getBean("tsbalanceService");    Map<Strin

15、g, Object> data = tsbalanceService.showTSBalance(map);    List<TSBalance> projectNodeList = (List<TSBalance>) data            .get("tsbalanceList"); &

16、#160;   String path = request.getRealPath("/excelTemplate").replace("",            "/");    String filePath = path + "/" 

17、;+ fileName;    File file = new File(filePath);     InputStream is = null;    try         is = new FileInputStream(file);  &#

18、160;     Workbook wb = new HSSFWorkbook(is);        int sheetsNum = wb.getNumberOfSheets();        /找到對應的sheet        fo

19、r (int i = 0; i < sheetsNum; i+)             String sheetName = wb.getSheetName(i);            int lastRowNum

20、60;= 0;            /如果是平衡報表            if (CommonUtil.SHEET_BALANCE.equals(sheetName)              &

21、#160;  Sheet sheet = wb.getSheet(sheetName);                lastRowNum = sheet.getLastRowNum();               

22、 /循環row,獲得需要的cell                for (int r = 4; r <= lastRowNum; r+)                 &

23、#160;   Row row = sheet.getRow(r);                    /電表地址所在的單元格                 

24、60;  Cell meterCell = row.getCell(4);                    /正向有功起碼所在的單元格,獲得cell的樣式                

25、;    Cell PP1Cell = row.getCell(7);                    CellStyle PP1CellStyle = PP1Cell.getCellStyle();        

26、;            /正向有功止碼所在的單元格,獲得cell的樣式                    Cell PP2Cell = row.getCell(8);      

27、60;             CellStyle PP2CellStyle = PP2Cell.getCellStyle();                    /反向有功起碼所在的單元格,獲得cell的樣式  &

28、#160;                 Cell PP4Cell = row.getCell(11);                    CellStyle PP4CellStyle

29、 = PP4Cell.getCellStyle();                    /反向有功止碼所在的單元格,獲得cell的樣式                    C

30、ell PP5Cell = row.getCell(12);                    CellStyle PP5CellStyle = PP5Cell.getCellStyle();            

31、        /從電表地址單元格中取出值                    String meterValue = ""            

32、        /數值單元格取值                       if (meterCell.getCellType() = Cell.CELL_TYPE_NUMERIC)      &#

33、160;                  int cellValue = (int) meterCell                       

34、;         .getNumericCellValue();                        meterValue = Integer.toString(cellValue);    

35、60;                                   /文本單元格取值             &

36、#160;      else if (meterCell.getCellType() = Cell.CELL_TYPE_STRING)                         meterValue = meterCell

37、.getStringCellValue();                                          /從頁面傳的list中找到對應的電表地址,并將起碼、

38、止碼寫入                    for (TSBalance balance : projectNodeList)                   &

39、#160;     /如果模板中的電表地址和list中的電表地址相等,將起碼、止碼寫入對應cell中;                        String meterAddress = balance.getAddress()=null?"":bala

40、nce.getAddress();                        String pp1 = balance.getPP1()=null?"":balance.getPP1();          

41、;              String pp2 = balance.getPP2()=null?"":balance.getPP2();                      

42、60; String pp4 = balance.getPP4()=null?"":balance.getPP4();                        String pp5 = balance.getPP5()=null?"":bala

43、nce.getPP5();                        if (!"".equals(meterAddress)&& meterValue.equals(meterAddress)         

44、;                    /判斷是否為-1,不為-1就修改單元格的值                          &#

45、160; if (!CommonUtil.judge(pp1)                                 PP1Cell.setCellValue(pp1);      

46、;                          PP1Cell.setCellStyle(PP1CellStyle);                  

47、                                      if (!CommonUtil.judge(pp2)       

48、;                          PP2Cell.setCellValue(pp2);                   

49、60;            PP2Cell.setCellStyle(PP2CellStyle);                               

50、0;                         if (!CommonUtil.judge(pp4)                   

51、60;             PP4Cell.setCellValue(pp4);                                P

52、P4Cell.setCellStyle(PP4CellStyle);                                            &#

53、160;            if (!CommonUtil.judge(pp5)                                &

54、#160;PP5Cell.setCellValue(pp5);                                PP5Cell.setCellStyle(PP5CellStyle);       

55、                                                  

56、                                                  

57、       /如果是10kv線損表            else if (CommonUtil.SHEET_LINE_LOSS.equals(sheetName)                 Sheet 

58、;sheet = wb.getSheet(sheetName);                lastRowNum = sheet.getLastRowNum();                for (int r&

59、#160;= 2; r < lastRowNum; r+)                     Row row = sheet.getRow(r);            

60、0;       /電表地址所在的單元格                                         

61、;Cell meterCell = row.getCell(4);                    /電量類的標識的單元格                    C

62、ell typeCell = row.getCell(3);                    /本月止碼所在的單元格                    Cell

63、 checkCodeCell = row.getCell(6);                    CellStyle chcekCodeStyle = checkCodeCell.getCellStyle();          &#

64、160;         /本月起碼所在的單元格                    Cell startCodeCell = row.getCell(5);          

65、          CellStyle startCodeStyle = startCodeCell.getCellStyle();                            &

66、#160;            /從電表地址單元格中取出值                    String meterValue = ""       &

67、#160;            /數值單元格取值                       if (meterCell.getCellType() = Cell.CELL_TYPE_NUMERIC) 

68、60;                       int cellValue = (int) meterCell.getNumericCellValue();              &

69、#160;         meterValue = Integer.toString(cellValue);                               

70、0;        /文本單元格取值                    else if (meterCell.getCellType() = Cell.CELL_TYPE_STRING)       

71、60;                 meterValue = meterCell.getStringCellValue();                       

72、60;                  /取電量標識值                    int typeValue = 0;     

73、;               /數值單元格取值                       if (typeCell.getCellType() = Cell.CELL_TYPE_NUME

74、RIC)                         typeValue = (int) typeCell.getNumericCellValue();             

75、0;                          /文本單元格取值                    else if&#

76、160;(typeCell.getCellType() = Cell.CELL_TYPE_STRING)                         typeValue = Integer.parseInt(typeCell.getStringCellValue();   &

77、#160;                                      /取本月止碼          &

78、#160;         String checkCodeValue = ""                    /數值單元格取值          &#

79、160;                                 if (checkCodeCell.getCellType() = Cell.CELL_TYPE_NUMERIC)    &#

80、160;                    checkCodeValue = Double.toString(checkCodeCell.getNumericCellValue();                &

81、#160;                       /文本單元格取值                    else if (checkCodeC

82、ell.getCellType() = Cell.CELL_TYPE_STRING)                         checkCodeValue = checkCodeCell.getStringCellValue();      

83、                                    /將本月止碼移動到本月起碼            

84、;        startCodeCell.setCellType(Cell.CELL_TYPE_STRING);                    startCodeCell.setCellValue(checkCodeValue);      

85、60;             startCodeCell.setCellStyle(startCodeStyle);                    for (TSBalance balance : projectNode

86、List)                         String meterAddress = balance.getAddress()=null?"":balance.getAddress();        

87、                String pp2 = balance.getPP2()=null?"":balance.getPP2();                    

88、0;   String pp5 = balance.getPP5()=null?"":balance.getPP5();                        String pp8 = balance.getPP8()=null?"

89、":balance.getPP8();                        String pp10 = balance.getPP10()=null?"":balance.getPP10();        &

90、#160;                                        if (!"".equals(meterAddress)&&&#

91、160;meterValue.equals(meterAddress)                             if(CommonUtil.judge(pp2)           &

92、#160;                    checkCodeCell.setCellValue("");                      &#

93、160;     else                                /正向有功           

94、;                     if (typeValue = CommonUtil.TYPE_PP2)                    &#

95、160;                checkCodeCell.setCellType(Cell.CELL_TYPE_STRING);                         &

96、#160;          checkCodeCell.setCellValue(pp2);                                  

97、;  checkCodeCell.setCellStyle(chcekCodeStyle);                                        

98、0;                                               if(CommonUtil.ju

99、dge(pp5)                                checkCodeCell.setCellValue("");          

100、;                  else                               &

101、#160; /反向有功                                if (typeValue = CommonUtil.TYPE_PP5)       

102、;                              checkCodeCell.setCellType(Cell.CELL_TYPE_STRING);           

103、0;                        checkCodeCell.setCellValue(pp5);                    &#

104、160;               checkCodeCell.setCellStyle(chcekCodeStyle);                           &

105、#160;                                                 &

106、#160;                                       if(CommonUtil.judge(pp8)      &

107、#160;                         checkCodeCell.setCellValue("");                 &#

108、160;              else                                 /正向無功 

109、0;                              if (typeValue = CommonUtil.TYPE_PP8)           &

110、#160;                         checkCodeCell.setCellType(Cell.CELL_TYPE_STRING);                

111、                    checkCodeCell.setCellValue(pp8);                        

112、0;           checkCodeCell.setCellStyle(chcekCodeStyle);                               

113、60;                                                 

114、60;                                   if(CommonUtil.judge(pp10)          &#

115、160;                     checkCodeCell.setCellValue("");                     

116、60;      else                                 /反向無功         

117、                       if (typeValue = CommonUtil.TYPE_PP10)                  &#

118、160;                  checkCodeCell.setCellType(Cell.CELL_TYPE_STRING);                       &

119、#160;            checkCodeCell.setCellValue(pp10);                               

120、0;    checkCodeCell.setCellStyle(chcekCodeStyle);                                      

121、60;                                                 

122、60;                                            else     

123、;            msg = "你所提交的模板不可用"                break;              

124、60;             wb.write(response.getOutputStream();     catch (Exception e)         e.printStackTrace();     finally  &

125、#160;      try             if (is != null)                is.close();       

126、;  catch (IOException e)             e.printStackTrace();                out.clear();    out = pageContex

127、t.pushBody();%>public static HSSFWorkbook createExcel() throws IOException         HSSFWorkbook workbook = new HSSFWorkbook();        HSSFSheet sheet;  

128、      HSSFRow row;        HSSFCell cell;        sheet = workbook.createSheet();        row = sheet.createRow(0); &#

129、160;      cell = row.createCell(0);        cell.setCellValue("第一行第一列");        cell = row.createCell(1);        cell.set

130、CellValue("第一行第二列");        cell = row.createCell(2);        cell.setCellValue("第一行第三列");        cell = row.createCell(3);   

131、     cell.setCellValue("第一行第四列");        cell = row.createCell(4);        cell.setCellValue("第一行第五列");        cell =

132、0;row.createCell(5);        cell.setCellValue("第一行第六列");        for (int i = 0; i < 30000; i+)             row = sheet.createRow(i + 1);            cell = row.createCell(0);            cell.setCellValue("123");    

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論