poi读取文件,并获取相应属性

格式比较乱,请到 poi.apache.org下载最新的包,详细请参考POI帮助文档,特别是设置打印模式的页页边距,请先用模板设置好,然后现得到,用得到的页边距生成新的文件,另外读取的模板的列的宽度,实际生成后显示的效果不一致,有待改进.....另外还有一个是商业的导出到excel www.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. }  

你可能感兴趣的:(java)