使用POI从Excel中取出一行到String中

时间:2014-12-07 16:49:38

标签: java arraylist apache-poi

我遇到了一个问题。

我希望将所有文本放在一行(在不同的单元格中)并将其作为字符串添加到数组列表中(即每一行,ArrayList中的新元素),稍后我会将该行拆分为单个元素然后,我将把它们中的一些放入不同的变量中。

我从“http://howtodoinjava.com/2013/06/19/readingwriting-excel-files-in-java-poi-tutorial/

获取此代码

我想把每个单元格添加到一个临时数组中,然后在每次迭代之后我将它们作为一个字符串组合在一起。然而,当我尝试对每个单元格进行编号(以查看一切是如何工作的)时,这种情况就会失败,细胞就会到处都是。

任何支持都会很棒!!

这是我的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.*;

import org.apache.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.*;

import java.lang.Iterable;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
public class ReadExcelDemo {
    ArrayList<String> stringRow = new ArrayList();
    ArrayList<String> temp = new ArrayList();
    ArrayList<String> list = new ArrayList<String>();
    String listString = "";
    String x;
    String y;

    public void go() {
        try {
            FileInputStream file = new FileInputStream(new File(
                    "ratesheet_prefix.xlsx"));

            // Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            // Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(1);

            // Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            String test;
            // test.

            while (rowIterator.hasNext()) {temp.clear();
                Row row = rowIterator.next();
                // For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext())

                {temp.clear();
                    Cell cell = cellIterator.next();
                    // Check the cell type and format accordingly
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                        // System.out.print(cell.getNumericCellValue() + " ");
                        this.x = " " + cell.getNumericCellValue() + " ";
                        temp.add(x);

                        System.out.print(x);

                        break;
                    case Cell.CELL_TYPE_STRING:
                        this.y = " " + cell.getStringCellValue() + " ";
                        // System.out.print(cell.getStringCellValue() + " ");
                        System.out.print(y);
                        temp.add(y);
                        break;
                    }
                    /*
                    for (String s : temp) {
                        listString += s + "\t";
                    }
                    System.out.println(listString);
                    */

                }

                System.out.println(" ");

                stringRow.add(listString);

                // String z = x + y;
                // System.out.println(z);

                /*
                 * for (String s : temp) { listString += s + "\t"; }
                 * System.out.println(listString); temp.clear();
                 * stringRow.add(listString);
                 */}
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
         * for (String x:stringRow){ System.out.println(x); }
         */

    }

    public static void main(String[] args) {
        ReadExcelDemo main = new ReadExcelDemo();
        main.go();

    }
}

1 个答案:

答案 0 :(得分:0)

从你的问题中你不清楚你要做什么。我打算猜一下 - 产生一个字符串数组,每行一个字符串,每个单元格的文本之间只有-,只有第一张表中的数据。

假设你基本上想要这样的东西,你需要做的就是:

List<String> text = new ArrayList<String>();

Workbook wb = WorkbookFactory.create(new File("input.xlsx"));
DataFormatter fmt = new DataFormatter();
Sheet s = wb.getSheetAt(0);

for (Row r : s) {
   StringBuffer sb = new StringBuffer();
   for (Cell c : r) {
      if (sb.length() > 0) sb.append(" - ");
      sb.append(fmt.formatCell(c));
   }
   text.add(sb.toString());
}

简单,不是吗!