Java 使用POI将Excel中的行转换为字符串

Java 使用POI将Excel中的行转换为字符串,java,arraylist,apache-poi,Java,Arraylist,Apache Poi,我遇到了一个问题 我希望获取一行(不同单元格中)上的所有文本,并将其作为字符串(即每行,ArrayList中的新元素)添加到数组列表中,稍后我会将该行拆分为单独的元素,然后将其中一些元素放入不同的变量中 我已将此代码从“ 我曾想过将每个单元格添加到一个临时数组中,在每次迭代之后,我将其作为一个字符串组合在一起。然而,这是失败的,因为当我尝试对每个单元格进行编号时(为了查看每个单元格是如何工作的),单元格到处都是 任何支持都会很好 这是我的密码: import java.io.BufferedRe

我遇到了一个问题

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

我已将此代码从“

我曾想过将每个单元格添加到一个临时数组中,在每次迭代之后,我将其作为一个字符串组合在一起。然而,这是失败的,因为当我尝试对每个单元格进行编号时(为了查看每个单元格是如何工作的),单元格到处都是

任何支持都会很好

这是我的密码:

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.io.BufferedReader;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.PrintStream;
导入java.text.DecimalFormat;
导入java.util.*;
导入org.apache.*;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.xssf.usermodel.*;
导入java.lang.Iterable;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.XSSFCell;
公共类ReadExcelDemo{
ArrayList stringRow=新的ArrayList();
ArrayList temp=新的ArrayList();
ArrayList=新建ArrayList();
字符串listString=“”;
字符串x;
弦y;
公开作废go(){
试一试{
FileInputStream文件=新FileInputStream(新文件(
"差饷租值表(prefix.xlsx);;
//创建包含对.xlsx文件引用的工作簿实例
XSSF工作簿=新XSSF工作簿(文件);
//从工作簿中获取第一张/所需的工作表
XSSFSheet sheet=workbook.getSheetAt(1);
//逐个遍历每一行
迭代器rowIterator=sheet.Iterator();
串试验;
//测试。
while(roweiterator.hasNext()){temp.clear();
行=行迭代器。下一步();
//对于每一行,遍历所有列
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext())
{temp.clear();
Cell=cellIterator.next();
//检查相应的单元格类型和格式
开关(cell.getCellType()){
case Cell.Cell\u类型\u数值:
//System.out.print(cell.getNumericCellValue()+);
this.x=“”+cell.getNumericCellValue()+”;
温度加成(x);
系统输出打印(x);
打破
case Cell.Cell\u类型\u字符串:
this.y=“”+cell.getStringCellValue()+”;
//System.out.print(cell.getStringCellValue()+);
系统输出打印(y);
温度添加(y);
打破
}
/*
用于(字符串s:temp){
listString+=s+“\t”;
}
System.out.println(listString);
*/
}
System.out.println(“”);
添加(listString);
//字符串z=x+y;
//系统输出打印ln(z);
/*
*对于(字符串s:temp){listString+=s+“\t”;}
*System.out.println(listString);temp.clear();
*添加(listString);
*/}
file.close();
}捕获(例外e){
e、 printStackTrace();
}
/*
*for(stringx:stringRow){System.out.println(x);}
*/
}
公共静态void main(字符串[]args){
ReadExcelDemo main=新的ReadExcelDemo();
main.go();
}
}

从你的问题中不太清楚你想做什么。不过我想猜测一下-生成一个字符串数组,每行一个字符串,每个单元格的文本之间有一个
-
,并且只包含第一页的数据

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

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());
}
List text=new ArrayList();
工作簿wb=WorkbookFactory.create(新文件(“input.xlsx”);
DataFormatter fmt=新的DataFormatter();
表s=wb.getSheetAt(0);
用于(r:s行){
StringBuffer sb=新的StringBuffer();
用于(单元c:r){
如果(sb.length()>0)sb.append(“-”);
sb.追加(fmt.formatCell(c));
}
添加(sb.toString());
}

很简单,不是吗!

问题到底是什么?从上面看不清楚。这实际上并没有编译。你确定代码是正确的吗?(例如,你有文本..但是追加不是文本的选项).Bah,这就是我把几个例子拼凑成一个简单答案的结果…现在试试-列表是
add
而不是
append
,这对我很有效。谢谢。