日期:2014-05-17  浏览次数:20718 次

如何用Apache POI读取Excel的单元格自定义名称的值。
如何用Apache POI读取Excel的单元格自定义名称的值所在位置。


我想根据Excel模板去生成一个带有数据的Excel文档,用单元格自定义名称为EMP_WEB所在位置来当做Excel单元格的坐标,而不是在代码中写死,如:1行,2列,如何用Apache POI读取Excel的名称框里的值呢?
------解决方案--------------------
没看明白,是想通过POI将数据库的数据读取到excel中,还是想读取excel中单元格所在值的坐标位置值?
------解决方案--------------------
poi 我和方面不是太方便

可以看看这一页http://poi.apache.org/spreadsheet/quick-guide.html#NamedRanges

要一个其中的例子

    // Setup code
    String cellName = "TestName";
    Workbook wb = getMyWorkbook(); // retrieve workbook

    // Retrieve the named range
    // Will be something like "$C$10,$D$12:$D$14";
    int namedCellIdx = wb.getNameIndex(cellName);
    Name aNamedCell = wb.getNameAt(namedCellIdx);

    // Retrieve the cell at the named range and test its contents
    // Will get back one AreaReference for C10, and
    //  another for D12 to D14
    AreaReference[] arefs = AreaReference.generateContiguous(aNamedCell.getRefersToFormula());
    for (int i=0; i<arefs.length; i++) {
        // Only get the corners of the Area
        // (use arefs[i].getAllReferencedCells() to get all cells)
        CellReference[] crefs = arefs[i].getCells();
        for (int j=0; j<crefs.length; j++) {
            // Check it turns into real stuff
            Sheet s = wb.getSheet(crefs[j].getSheetName());
            Row r = s.getRow(crefs[j].getRow());
            Cell c = r.getCell(crefs[j].getCol());
            // Do something with this corner cell
        }
    }