Apache POI库操作Excel 2007文档

来源:互联网 发布:php的特点与优势 编辑:程序博客网 时间:2024/05/23 10:51

准备

从官网上下载到bin, src包后,解压,并导入一个IDEA Java工程后。使用如下代码读取:

            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(FILE_TO_BE_READ));            XSSFSheet sheet = workbook.getSheet("Sheet1");            XSSFRow row = sheet.getRow(0);

XSSFSheet

XSSFWorkbook对象中用一个List来存储所有Sheet.

private List<XSSFSheet> sheets;

XSSFRow的研究

XSSFRow采用一个TreeMap存储一行中的cells, TreeMap保证了数据按照Column Index顺序存储。java.util.TreeMap是基于红黑树的数据结构,接近平衡的二叉查找树,它能保证get, put, remove, containsKey等操作有log(n)的时间复杂度。

private final TreeMap<Integer, XSSFCell> _cells;


遍历所有cells
            for(Row row : sheet) {                System.out.println("Physical cell count: " + row.getPhysicalNumberOfCells());                for(Cell cell : row) {                    int colIndex = cell.getColumnIndex();                    System.out.println("colIndex :" + colIndex + " Value:" + cell.getStringCellValue());                }            }

经验证,使用InputStream实例化XSSFWorkbook对象时,全部文件内容被加载到内存中。在遍历Excel文件中的cell时,不需要再次读取文件。


Excel Header and Footer

Headers and footers are lines of text that print at the top (header) and bottom (footer) of each page in the spreadsheet.They contain descriptive text such as titles, dates, and/or page numbers. They are used to add information to a spreadsheet that is being printed.
Headers and footers are not visible in the normal worksheet view.To view a header or footer before printing the spreadsheet, use the Print Preview option.


0 0
原创粉丝点击