2

これが私がやっていることの要点です。

コンストラクターで指定したファイルを取得する ExcelProcessor オブジェクトを作成しています。この後、本、行、そして行内のセルを取得します。最初の行には、各列がデータに関して保持するものの名​​前があります。残念ながら、セルが連続して空白の場合、その場で処理が停止します。各行のX個の列を読むと言う方法はありますか?

ありがとう!

public class ExcelProcessor
{
    private static File xslFile;

    ExcelProcessor( String file )
    {
        xslFile = new File(file);
    }

    /**
     * creates an {@link HSSFWorkbook} the specified OS filename.
     */

    /**
     * Returns an XSSF Workbook object that can be queried. 
     * 
     * This method either returns a Workbook with the specified
     * file location or it will throw an IO Error.
     *
     * @return      the Excel Workbook 
     * @see         XSSFWB
     */

    private XSSFWorkbook readFile() throws IOException
    {
        return new XSSFWorkbook(new FileInputStream(xslFile));
    }

    /**
     * Returns all rows of the XSSF Workbook that is read in via the file system.
     * 
     * This method returns a XSSFRow array for processing
     *
     * @param   wb  the Excel workbook located on the file system
     * @return      Rows contained within the Excel Workbook
     * @see         XSSFRows
     */

    private XSSFRow[] retrieveRows(XSSFWorkbook wb)
    {
        XSSFSheet xslSheet = wb.getSheetAt(0);
        XSSFRow[] sheetRows = new XSSFRow[xslSheet.getLastRowNum()];

        for (int i = 0; i < xslSheet.getLastRowNum(); i++)
        {
            sheetRows[i] = xslSheet.getRow(i);
        }

        return sheetRows;
    }

    private XSSFWorkbook modifyRows(XSSFWorkbook wb)
    {
        return wb;
    }

    /**
     * Processes all rows of the XSSF Workbook that is read in via the file system.
     * Each Cell is pulled from the rows to create a structure needed for WQS 
     * Web Services.
     * 
     * This method returns a String array for all Cells pulled from the Rows
     *
     * @param   xslRow  Current row pulled from the XSL Workbook
     * @return      String Array of all Row Cell Values
     * @see         String[]
     */

    public String[] processRow(XSSFRow xslRow)
    {
        int totalCells = xslRow.getPhysicalNumberOfCells();
        System.out.println("Total Cells: " + totalCells);
        String[] cellValues = new String[totalCells];

        for (int i = 0; i < totalCells; i++)
        {
            XSSFCell currentCell = xslRow.getCell(i);
            String cellVal;
            try
            {
                cellVal = currentCell.getStringCellValue();
            }
            catch (Exception e)
            {
                System.out.println("Empty Cell");
                cellVal = "";
            }
            cellValues[i] = cellVal;
            System.out.println(cellVal);
        }

        return cellValues;
    }

    /**
     * Main method which creates an ExcelProcessor before handling the Excel
     * Spreadsheet given to the user.
     * 
     * @param args
     * @throws ParserConfigurationException
     * @throws SAXException
     */

    public static void main(String args[]) throws ParserConfigurationException, SAXException
    {
        ExcelProcessor driver = new ExcelProcessor("D:\\TestBook.xlsx");

        try
        {
            XSSFWorkbook xslWB = driver.readFile();
            XSSFRow[] xslRows = driver.retrieveRows(xslWB);
            ArrayList<String[]> rowCellVals = new ArrayList<String[]>();

            for (int r = 0; r < xslRows.length; r++)
            {
                rowCellVals.add(driver.processRow(xslRows[r]));
            }

            String sessionKey = WQSServices.sessionToken();
            System.out.println("Have the Session Key: " + sessionKey);
            WQSServices.uploadAttachment(sessionKey, rowCellVals);

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}
4

2 に答える 2

2

代わりにgetLastCellNumを使用しgetPhysicalNumberOfCells、セルの数を返します (空のセルは数えません)

于 2012-12-20T20:10:33.963 に答える