【问题标题】:Taking a row from Excel into a String using POI使用 POI 将 Excel 中的一行转换为字符串
【发布时间】:2014-12-07 16:49:37
【问题描述】:

我遇到了问题。

我希望获取一行中的所有文本(在不同的单元格中)并将其作为字符串添加到数组列表中(即每一行,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();

    }
}

【问题讨论】:

  • 到底是什么问题?从上面看不清楚。

标签: java arraylist apache-poi


【解决方案1】:

您的问题不太清楚您要做什么。不过我会猜测一下 - 生成一个字符串数组,每行一个字符串,每个单元格的文本之间有一个-,并且只有第一张表中的数据。

假设您基本上想要类似的东西,您需要做的就是:

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());
}

很简单,不是吗!

【讨论】:

  • 这实际上并没有编译。你确定代码是正确的吗? (例如你有文本..但是追加不是文本的选项)。
  • 呸,这就是我将几个例子拼凑成一个简单答案的结果......现在试试 - 列表是add而不是append
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 2011-11-28
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多