格式比较乱,请到
poi.apache.org下载最新的包,详细请参考POI帮助文档,特别是设置打印模式的页页边距,请先用模板设置好,然后现得到,用得到的页边距生成新的文件,另外读取的模板的列的宽度,实际生成后显示的效果不一致,有待改进.....另外还有一个是商业的导出到excel
www.jxcell.net/
java 代码
java 代码
- package www.proudsoul.poi;
- import java.io.FileInputStream;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- import org.apache.poi.hssf.usermodel.HSSFComment;
- import org.apache.poi.hssf.usermodel.HSSFFooter;
- import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
- import org.apache.poi.hssf.usermodel.HSSFRichTextString;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.poifs.filesystem.POIFSFileSystem;
- public class ReaderDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- try {
- // create a poi workbook from the excel spreadsheet file
- POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
- "demo.xls"));
- // POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
- // "E:/JavaExcel/poi-3.0-rc4/src/testcases/org/apache/poi/hssf/data/12843-1.xls"));
- HSSFWorkbook wb = new HSSFWorkbook(fs);
- for (int k = 0; k < wb.getNumberOfSheets(); k++) {
- HSSFSheet sheet = wb.getSheetAt(k);
- System.out.println("列宽: "+sheet.getColumnWidth((short)0));
- HSSFFooter footer = sheet.getFooter();
- int rows = sheet.getPhysicalNumberOfRows();
- if (rows > 0) {
- System.out.println("总共: " + rows);
- System.out.println("当前时间: " + HSSFFooter.date()
- + " 当前文件名称: " + HSSFFooter.file() + " 页数: "
- + HSSFFooter.numPages() + " 当前工作表名称: "
- + HSSFFooter.tab());
- sheet.getMargin(HSSFSheet.TopMargin);
- HSSFPrintSetup hps = sheet.getPrintSetup();
- /*System.out.println("得到打印模式: " + hps.getLandscape()
- + " 页面大小: " + hps.getPaperSize() + " 头部空白: "
- + sheet.getMargin(HSSFSheet.TopMargin) + " 左部空白: "
- + sheet.getMargin(HSSFSheet.LeftMargin) + " 右部空白: "
- + sheet.getMargin(HSSFSheet.RightMargin)
- + " 底部空白: "
- + sheet.getMargin(HSSFSheet.BottomMargin)
- * + "
- * PANE_LOWER_LEFT: "
- * +sheet.getMargin(HSSFSheet.PANE_LOWER_LEFT)+ "
- * PANE_LOWER_RIGHT:
- * "+sheet.getMargin(HSSFSheet.PANE_LOWER_RIGHT)+ "
- * PANE_UPPER_LEFT: "
- * +sheet.getMargin(HSSFSheet.PANE_UPPER_LEFT)+ "
- * PANE_UPPER_RIGHT: "
- * +sheet.getMargin(HSSFSheet.PANE_UPPER_RIGHT)+ "
- * 居中方式:
- * "+sheet.getHorizontallyCenter()
- );*/
- for (int r = 0; r < rows; r++) {
- /* 得到一行的单元格内容 */
- HSSFRow row = sheet.getRow(r);
- if (row != null) {
- System.out.println("当前行: "+row.getRowNum()+ " 高度:"+row.getHeight()+ " 宽度: "+row.getLastCellNum()+ "HeightInPoints: "+row.getHeightInPoints()+ "当前表格的大小: "+row.getPhysicalNumberOfCells()+ "FirstCellNum: "+row.getFirstCellNum()+ "ZeroHeight: "+row.getZeroHeight());
- /* 得到一行中每个单元格的内容 */
- int cells = row.getPhysicalNumberOfCells();
- for (short c = 0; c < cells; c++) {
- HSSFCell cell = row.getCell(c);
- HSSFCellStyle cellstyle = cell.getCellStyle();
- System.out.println(/*"Alignment: "+cellstyle.getAlignment()+
- " BorderBottom: "+cellstyle.getBorderBottom()+
- " Alignment: "+cellstyle.getBorderLeft() +
- " BorderRight "+cellstyle.getBorderRight()+
- " BorderTop "+cellstyle.getBorderTop() +
- " BottomBorderColor "+cellstyle.getBottomBorderColor() +
- " DataFormat "+cellstyle.getDataFormat() +
- " FillBackgroundColor "+cellstyle.getFillBackgroundColor() +
- " FillForegroundColor "+cellstyle.getFillForegroundColor() +
- " FillPattern "+cellstyle.getFillPattern() +*/
- " FontIndex "+cellstyle.getFontIndex() /*+
- " Hidden "+cellstyle.getHidden() +
- " Indention "+cellstyle.getIndention() +
- " Index "+cellstyle.getIndex() +
- " LeftBorderColor "+cellstyle.getLeftBorderColor() +
- " Locked "+cellstyle.getLocked() +
- " RightBorderColor "+cellstyle.getRightBorderColor() +
- " Rotation "+cellstyle.getRotation() +
- " TopBorderColor "+cellstyle.getTopBorderColor() +
- " VerticalAlignment "+cellstyle.getVerticalAlignment() +
- " WrapText "+cellstyle.getWrapText()*/ );
- HSSFComment comment = cell.getCellComment();
- /*System.out.println("当前列: "
- + cell.getCellNum());*/
- if (cell != null) {
- String value = null;
- switch (cell.getCellType()) {
- /*
- * case HSSFCell.CELL_TYPE_FORMULA: value =
- * "FORMULA "; break;
- */
- case HSSFCell.CELL_TYPE_NUMERIC:
- value = " "
- + cell.getNumericCellValue();
- break;
- /* 此行表示单元格的内容为string类型 */
- case HSSFCell.CELL_TYPE_STRING:
- value = " "
- + cell.getRichStringCellValue();
- System.out.print(value);
- break;
- /* 此行表示该单元格值为空 */
- case HSSFCell.CELL_TYPE_BLANK:
- break;
- /*
- * case HSSFCell.CELL_TYPE_BOOLEAN: value =
- * "BOOLEAN=" + cell.getBooleanCellValue();
- *
- * break;
- */
- /*
- * case HSSFCell.CELL_TYPE_ERROR: value =
- * "ERROR=" + cell.getErrorCellValue();
- *
- * break;
- */
- default:
- }
- }
- }
- System.out.println();
- }
- }
- System.out.println();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }