poi讀取文件,並獲取相應屬性 (java操作Excel文件)

關鍵字:   poi    
格式比較亂,請到poi.apache.org下載最新的包,詳細請參考POI幫助文檔,特別是設置打印模式的頁頁邊距,請先用模板設置好,然後現得到,用得到的頁邊距生成新的文件,另外讀取的模板的列的寬度,實際生成後顯示的效果不一致,有待改進.....另外還有一個是商業的導出到excelwww.jxcell.net/
java 代碼
  1. package www.proudsoul.poi;  
  2.   
  3. import java.io.FileInputStream;  
  4.   
  5. import org.apache.poi.hssf.usermodel.HSSFCell;  
  6. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  7. import org.apache.poi.hssf.usermodel.HSSFComment;  
  8. import org.apache.poi.hssf.usermodel.HSSFFooter;  
  9. import org.apache.poi.hssf.usermodel.HSSFPrintSetup;  
  10. import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
  11. import org.apache.poi.hssf.usermodel.HSSFRow;  
  12. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  13. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  14. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  15.   
  16. public class ReaderDemo {  
  17.   
  18.     /** 
  19.      * @param args 
  20.      */  
  21.     public static void main(String[] args) {  
  22.         try {  
  23.             // create a poi workbook from the excel spreadsheet file  
  24.             POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(  
  25.                     "demo.xls"));  
  26.             // POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(  
  27.             // "E:/JavaExcel/poi-3.0-rc4/src/testcases/org/apache/poi/hssf/data/12843-1.xls"));  
  28.             HSSFWorkbook wb = new HSSFWorkbook(fs);  
  29.             for (int k = 0; k < wb.getNumberOfSheets(); k++) {  
  30.                 HSSFSheet sheet = wb.getSheetAt(k);  
  31.                 System.out.println("列寬: "+sheet.getColumnWidth((short)0));  
  32.                 HSSFFooter footer = sheet.getFooter();  
  33.                 int rows = sheet.getPhysicalNumberOfRows();  
  34.                 if (rows > 0) {  
  35.                     System.out.println("總共: " + rows);  
  36.                     System.out.println("當前時間: " + HSSFFooter.date()  
  37.                             + " 當前文件名稱: " + HSSFFooter.file() + " 頁數: "  
  38.                             + HSSFFooter.numPages() + " 當前工作表名稱: "  
  39.                             + HSSFFooter.tab());  
  40.                     sheet.getMargin(HSSFSheet.TopMargin);  
  41.                     HSSFPrintSetup hps = sheet.getPrintSetup();  
  42.                     /*System.out.println("得到打印模式: " + hps.getLandscape() 
  43.                             + " 頁面大小: " + hps.getPaperSize() + " 頭部空白: " 
  44.                             + sheet.getMargin(HSSFSheet.TopMargin) + " 左部空白: " 
  45.                             + sheet.getMargin(HSSFSheet.LeftMargin) + " 右部空白: " 
  46.                             + sheet.getMargin(HSSFSheet.RightMargin) 
  47.                             + " 底部空白: " 
  48.                             + sheet.getMargin(HSSFSheet.BottomMargin) 
  49.                                                                          * + " 
  50.                                                                          * PANE_LOWER_LEFT: " 
  51.                                                                          * +sheet.getMargin(HSSFSheet.PANE_LOWER_LEFT)+ " 
  52.                                                                          * PANE_LOWER_RIGHT: 
  53.                                                                          * "+sheet.getMargin(HSSFSheet.PANE_LOWER_RIGHT)+ " 
  54.                                                                          * PANE_UPPER_LEFT: " 
  55.                                                                          * +sheet.getMargin(HSSFSheet.PANE_UPPER_LEFT)+ " 
  56.                                                                          * PANE_UPPER_RIGHT: " 
  57.                                                                          * +sheet.getMargin(HSSFSheet.PANE_UPPER_RIGHT)+ " 
  58.                                                                          * 居中方式: 
  59.                                                                          * "+sheet.getHorizontallyCenter() 
  60.                                                                           
  61.                     );*/  
  62.   
  63.                     for (int r = 0; r < rows; r++) {  
  64.                         /* 得到一行的單元格內容 */  
  65.                         HSSFRow row = sheet.getRow(r);  
  66.                         if (row != null) {  
  67.                               
  68.                              System.out.println("當前行: "+row.getRowNum()+ " 高度:"+row.getHeight()+ " 寬度: "+row.getLastCellNum()+ "HeightInPoints: "+row.getHeightInPoints()+ "當前表格的大小: "+row.getPhysicalNumberOfCells()+ "FirstCellNum: "+row.getFirstCellNum()+ "ZeroHeight: "+row.getZeroHeight());  
  69.                                
  70.   
  71.                             /* 得到一行中每個單元格的內容 */  
  72.                             int cells = row.getPhysicalNumberOfCells();  
  73.                             for (short c = 0; c < cells; c++) {  
  74.                                 HSSFCell cell = row.getCell(c);  
  75.                                 HSSFCellStyle cellstyle = cell.getCellStyle();  
  76.                                 System.out.println(/*"Alignment: "+cellstyle.getAlignment()+ 
  77.                                         " BorderBottom: "+cellstyle.getBorderBottom()+ 
  78.                                         " Alignment: "+cellstyle.getBorderLeft() + 
  79.                                         " BorderRight "+cellstyle.getBorderRight()+  
  80.                                         " BorderTop "+cellstyle.getBorderTop() + 
  81.                                         " BottomBorderColor "+cellstyle.getBottomBorderColor() + 
  82.                                         " DataFormat "+cellstyle.getDataFormat() + 
  83.                                         " FillBackgroundColor "+cellstyle.getFillBackgroundColor() + 
  84.                                         " FillForegroundColor "+cellstyle.getFillForegroundColor() + 
  85.                                         " FillPattern "+cellstyle.getFillPattern() +*/  
  86.                                         " FontIndex "+cellstyle.getFontIndex() /*+ 
  87.                                         " Hidden "+cellstyle.getHidden() + 
  88.                                         " Indention "+cellstyle.getIndention() + 
  89.                                         " Index "+cellstyle.getIndex() + 
  90.                                         " LeftBorderColor "+cellstyle.getLeftBorderColor() + 
  91.                                         " Locked "+cellstyle.getLocked() + 
  92.                                         " RightBorderColor "+cellstyle.getRightBorderColor() + 
  93.                                         " Rotation "+cellstyle.getRotation() + 
  94.                                         " TopBorderColor "+cellstyle.getTopBorderColor() + 
  95.                                         " VerticalAlignment "+cellstyle.getVerticalAlignment() + 
  96.                                         " WrapText "+cellstyle.getWrapText()*/ );  
  97.                                 HSSFComment comment = cell.getCellComment();  
  98.                                 /*System.out.println("當前列: " 
  99.                                         + cell.getCellNum());*/  
  100.   
  101.                                 if (cell != null) {  
  102.                                     String value = null;  
  103.                                     switch (cell.getCellType()) {  
  104.                                     /* 
  105.                                      * case HSSFCell.CELL_TYPE_FORMULA: value = 
  106.                                      * "FORMULA "; break; 
  107.                                      */  
  108.                                     case HSSFCell.CELL_TYPE_NUMERIC:  
  109.                                         value = "   "  
  110.                                                 + cell.getNumericCellValue();  
  111.                                         break;  
  112.                                     /* 此行表示單元格的內容爲string類型 */  
  113.                                     case HSSFCell.CELL_TYPE_STRING:  
  114.                                         value = "  "  
  115.                                                 + cell.getRichStringCellValue();  
  116.                                         System.out.print(value);  
  117.   
  118.                                         break;  
  119.                                     /* 此行表示該單元格值爲空 */  
  120.                                     case HSSFCell.CELL_TYPE_BLANK:  
  121.                                         break;  
  122.                                     /* 
  123.                                      * case HSSFCell.CELL_TYPE_BOOLEAN: value = 
  124.                                      * "BOOLEAN=" + cell.getBooleanCellValue(); 
  125.                                      *  
  126.                                      * break; 
  127.                                      */  
  128.                                     /* 
  129.                                      * case HSSFCell.CELL_TYPE_ERROR: value = 
  130.                                      * "ERROR=" + cell.getErrorCellValue(); 
  131.                                      *  
  132.                                      * break; 
  133.                                      */  
  134.                                     default:  
  135.                                     }  
  136.                                 }  
  137.                             }  
  138.                             System.out.println();  
  139.                         }  
  140.                     }  
  141.                     System.out.println();  
  142.                 }  
  143.             }  
  144.         } catch (Exception e) {  
  145.             e.printStackTrace();  
  146.         }  
  147.     }  
  148. }  
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章