如何从 Excel 文件中逐行读取值,并将它们放入地图中?
How to read values line by line from Excel file, and place them into a Map?
我的目标是阅读具有已知格式的 excel 文档,一次阅读每个单元格和列。每行将被设置到一个名为 Item 的 bean 中,它将被放入一个映射中,其键作为项目编号。一旦我这样做了,我就可以继续程序的其余部分并放入数据库中。我目前一次写一个完整的专栏,并试图弄清楚如何做到这一点。
public class Excel {
private Items items;
Excel(Items items) {
this.items = items;
}
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser();
int returnVlue = fileChooser.showOpenDialog(null);
Map<Long, Items> map1 = new HashMap<Long, Items>();
String mat, thick, size, lbsPerSheet, lbs;
if (returnVlue == JFileChooser.APPROVE_OPTION) {
try {
Workbook workbook = new HSSFWorkbook(new FileInputStream(
fileChooser.getSelectedFile()));
Sheet sheet = workbook.getSheetAt(0);
for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
Row row = rit.next();
for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
Cell cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
mat = cell.getStringCellValue();
thick = cell.getStringCellValue();
size = cell.getStringCellValue();
lbsPerSheet = cell.getStringCellValue();
lbs = cell.getStringCellValue();
System.out.println("Mat "+mat+" Thick "+ thick+" Size "+size+" lbs Per Sheet "+lbsPerSheet+" lbs "+lbs+ "\t");
}
System.out.println();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
输出
Mat 11960120 Thick 11960120 Size 11960120 lbs Per Sheet 11960120 lbs 11960120
Mat 0.119 Thick 0.119 Size 0.119 lbs Per Sheet 0.119 lbs 0.119
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 250 Thick 250 Size 250 lbs Per Sheet 250 lbs 250
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 18860120 Thick 18860120 Size 18860120 lbs Per Sheet 18860120 lbs 18860120
Mat 0.188 Thick 0.188 Size 0.188 lbs Per Sheet 0.188 lbs 0.188
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 383 Thick 383 Size 383 lbs Per Sheet 383 lbs 383
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 25060120 Thick 25060120 Size 25060120 lbs Per Sheet 25060120 lbs 25060120
Mat 0.25 Thick 0.25 Size 0.25 lbs Per Sheet 0.25 lbs 0.25
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 510 Thick 510 Size 510 lbs Per Sheet 510 lbs 510
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 25072144 Thick 25072144 Size 25072144 lbs Per Sheet 25072144 lbs 25072144
Mat 0.25 Thick 0.25 Size 0.25 lbs Per Sheet 0.25 lbs 0.25
Mat 72x144 Thick 72x144 Size 72x144 lbs Per Sheet 72x144 lbs 72x144
Mat 734.4 Thick 734.4 Size 734.4 lbs Per Sheet 734.4 lbs 734.4
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 31360120 Thick 31360120 Size 31360120 lbs Per Sheet 31360120 lbs 31360120
Mat 0.313 Thick 0.313 Size 0.313 lbs Per Sheet 0.313 lbs 0.313
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 633.2616 Thick 633.2616 Size 633.2616 lbs Per Sheet 633.2616 lbs 633.2616
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 37560120 Thick 37560120 Size 37560120 lbs Per Sheet 37560120 lbs 37560120
Mat 0.375 Thick 0.375 Size 0.375 lbs Per Sheet 0.375 lbs 0.375
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 758.7 Thick 758.7 Size 758.7 lbs Per Sheet 758.7 lbs 758.7
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 50060120 Thick 50060120 Size 50060120 lbs Per Sheet 50060120 lbs 50060120
Mat 0.5 Thick 0.5 Size 0.5 lbs Per Sheet 0.5 lbs 0.5
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 1011.6 Thick 1011.6 Size 1011.6 lbs Per Sheet 1011.6 lbs 1011.6
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 75060120 Thick 75060120 Size 75060120 lbs Per Sheet 75060120 lbs 75060120
Mat 0.75 Thick 0.75 Size 0.75 lbs Per Sheet 0.75 lbs 0.75
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 1517.4 Thick 1517.4 Size 1517.4 lbs Per Sheet 1517.4 lbs 1517.4
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 10060120 Thick 10060120 Size 10060120 lbs Per Sheet 10060120 lbs 10060120
Mat 1 Thick 1 Size 1 lbs Per Sheet 1 lbs 1
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 2023.2 Thick 2023.2 Size 2023.2 lbs Per Sheet 2023.2 lbs 2023.2
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Excel 文件
11960120 0.119 60x120 250.00 [=13=].4100
18860120 0.188 60x120 383.00 [=13=].4100
25060120 0.250 60x120 510.00 [=13=].4100
25072144 0.250 72x144 734.40 [=13=].4100
31360120 0.313 60x120 633.26 [=13=].4100
37560120 0.375 60x120 758.70 [=13=].4100
50060120 0.500 60x120 1011.60 [=13=].4100
75060120 0.750 60x120 1517.40 [=13=].4100
10060120 1.000 60x120 2023.20 [=13=].4100
我想像阅读二维数组一样阅读上面的 excel 文件。例如:
array[0][0] = 11960120; array[0][1] = 0.119; array[0][2]= "60x120";
如果我可以像上面那样使用 getter 和 Items class 中的 setter 读取行和列,那么只需将 "Item" 放入地图中并用商品编号,例如 11960120 在上面 excel 示例中的单元格 [0][0] 中。
为了更清楚,我想像这样阅读 excel 文件
11960120 0.119 60x120 250.00 [=15=].4100
然后阅读下一行
18860120 0.188 60x120 383.00 [=16=].4100
每个单元格将被设置到项目中class。
您可以使用 Apache Metamodel 库来查询 Excel 电子表格。我在下面显示的方法保留了列名,因此您可以编写一个方法来按列名和行号获取值。如果您没有 want/have 列名称,则必须调整此代码:
private static org.slf4j.Logger logger = LoggerFactory.getLogger( "Data" );
public static Object[][] getExcelData( File excelFile, String sheetName )
{
ExcelConfiguration conf = new ExcelConfiguration( 1, true, false );
DataContext dataContext = DataContextFactory.createExcelDataContext( excelFile, conf );
DataSet dataSet = dataContext.query()
.from( sheetName )
.selectAll()
.where("run").eq("Y")
.execute();
List<Row> rows = dataSet.toRows();
Object[][] myArray = get2ArgArrayFromRows( rows );
return myArray;
}
/**
* Gets a 2D Object array from a List of Row objects that is only 2 args wide.
* @param rows
* @return
*/
public static Object[][] get2ArgArrayFromRows( List<Row> rows ) {
Object[][] myArray = new Object[rows.size()][2];
int i = 0;
SelectItem[] cols = rows.get(0).getSelectItems();
for ( Row r : rows ) {
Object[] data = r.getValues();
for ( int j = 0; j < cols.length; j++ ) {
if ( data[j] == null ) data[j] = ""; // force empty string where there are NULL values
}
myArray[i][0] = cols;
myArray[i][2] = data;
i++;
}
logger.info( "Row count: " + rows.size() );
logger.info( "Column names: " + Arrays.toString( cols ) );
return myArray;
}
然后,如果需要,可以使用 Google Guava 将二维数组转换为列表。 (我认为如果有欺骗,转换为 Map 会有问题吗?):
Double[][] array;
List<List<Double>> list = Lists.transform(Arrays.asList(array),
new Function<Double[], List<Double>>() {
@Override public List<Double> apply(Double[] row) {
return Arrays.asList(row);
}
}
}
您当前的循环:
for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
Row row = rit.next();
for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
Cell cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
mat = cell.getStringCellValue();
thick = cell.getStringCellValue();
size = cell.getStringCellValue();
lbsPerSheet = cell.getStringCellValue();
lbs = cell.getStringCellValue();
System.out.println("Mat "+mat+" Thick "+ thick+" Size "+size+" lbs Per Sheet "+lbsPerSheet+" lbs "+lbs+ "\t");
}
System.out.println();
}
...基本上是说:"For each row and each cell in this sheet, put the value of that particular cell into these five different variables"。 cell
是当前单元格,获取它的字符串值并将其放入所有变量中实际上没有意义。您可能希望第一个单元格转到 mat
,第二个单元格转到 thick
等等。您可以执行以下两项操作之一:
不循环赋值:
for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
Row row = rit.next();
Iterator<Cell> cit = row.cellIterator()
Cell cell;
if ( cit.hasNext() ) {
cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
mat = cell.getStringCellValue();
}
if ( cit.hasNext() ) {
cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
thick = cell.getStringCellValue();
}
if ( cit.hasNext() ) {
cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
size = cell.getStringCellValue();
}
if ( cit.hasNext() ) {
cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
lbsPerSheet = cell.getStringCellValue();
}
if ( cit.hasNext() ) {
cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
lbs = cell.getStringCellValue();
}
System.out.println("Mat "+mat+" Thick "+ thick+" Size "+size+" lbs Per Sheet "+lbsPerSheet+" lbs "+lbs+ "\n");
}
使用数组并循环:
for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
Row row = rit.next();
String[] cells = new String[row.getPhysicalNumberOfCells()];
int i = 0;
for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
Cell cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
cells[i++] = cell.getStringCellValue();
}
// At this point you can put the values of the cells in
// your map entry.
System.out.println(Arrays.toString(cells);
}
我的目标是阅读具有已知格式的 excel 文档,一次阅读每个单元格和列。每行将被设置到一个名为 Item 的 bean 中,它将被放入一个映射中,其键作为项目编号。一旦我这样做了,我就可以继续程序的其余部分并放入数据库中。我目前一次写一个完整的专栏,并试图弄清楚如何做到这一点。
public class Excel {
private Items items;
Excel(Items items) {
this.items = items;
}
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser();
int returnVlue = fileChooser.showOpenDialog(null);
Map<Long, Items> map1 = new HashMap<Long, Items>();
String mat, thick, size, lbsPerSheet, lbs;
if (returnVlue == JFileChooser.APPROVE_OPTION) {
try {
Workbook workbook = new HSSFWorkbook(new FileInputStream(
fileChooser.getSelectedFile()));
Sheet sheet = workbook.getSheetAt(0);
for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
Row row = rit.next();
for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
Cell cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
mat = cell.getStringCellValue();
thick = cell.getStringCellValue();
size = cell.getStringCellValue();
lbsPerSheet = cell.getStringCellValue();
lbs = cell.getStringCellValue();
System.out.println("Mat "+mat+" Thick "+ thick+" Size "+size+" lbs Per Sheet "+lbsPerSheet+" lbs "+lbs+ "\t");
}
System.out.println();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
输出
Mat 11960120 Thick 11960120 Size 11960120 lbs Per Sheet 11960120 lbs 11960120
Mat 0.119 Thick 0.119 Size 0.119 lbs Per Sheet 0.119 lbs 0.119
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 250 Thick 250 Size 250 lbs Per Sheet 250 lbs 250
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 18860120 Thick 18860120 Size 18860120 lbs Per Sheet 18860120 lbs 18860120
Mat 0.188 Thick 0.188 Size 0.188 lbs Per Sheet 0.188 lbs 0.188
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 383 Thick 383 Size 383 lbs Per Sheet 383 lbs 383
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 25060120 Thick 25060120 Size 25060120 lbs Per Sheet 25060120 lbs 25060120
Mat 0.25 Thick 0.25 Size 0.25 lbs Per Sheet 0.25 lbs 0.25
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 510 Thick 510 Size 510 lbs Per Sheet 510 lbs 510
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 25072144 Thick 25072144 Size 25072144 lbs Per Sheet 25072144 lbs 25072144
Mat 0.25 Thick 0.25 Size 0.25 lbs Per Sheet 0.25 lbs 0.25
Mat 72x144 Thick 72x144 Size 72x144 lbs Per Sheet 72x144 lbs 72x144
Mat 734.4 Thick 734.4 Size 734.4 lbs Per Sheet 734.4 lbs 734.4
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 31360120 Thick 31360120 Size 31360120 lbs Per Sheet 31360120 lbs 31360120
Mat 0.313 Thick 0.313 Size 0.313 lbs Per Sheet 0.313 lbs 0.313
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 633.2616 Thick 633.2616 Size 633.2616 lbs Per Sheet 633.2616 lbs 633.2616
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 37560120 Thick 37560120 Size 37560120 lbs Per Sheet 37560120 lbs 37560120
Mat 0.375 Thick 0.375 Size 0.375 lbs Per Sheet 0.375 lbs 0.375
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 758.7 Thick 758.7 Size 758.7 lbs Per Sheet 758.7 lbs 758.7
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 50060120 Thick 50060120 Size 50060120 lbs Per Sheet 50060120 lbs 50060120
Mat 0.5 Thick 0.5 Size 0.5 lbs Per Sheet 0.5 lbs 0.5
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 1011.6 Thick 1011.6 Size 1011.6 lbs Per Sheet 1011.6 lbs 1011.6
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 75060120 Thick 75060120 Size 75060120 lbs Per Sheet 75060120 lbs 75060120
Mat 0.75 Thick 0.75 Size 0.75 lbs Per Sheet 0.75 lbs 0.75
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 1517.4 Thick 1517.4 Size 1517.4 lbs Per Sheet 1517.4 lbs 1517.4
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Row org.apache.poi.hssf.usermodel.HSSFRow@2a
Mat 10060120 Thick 10060120 Size 10060120 lbs Per Sheet 10060120 lbs 10060120
Mat 1 Thick 1 Size 1 lbs Per Sheet 1 lbs 1
Mat 60x120 Thick 60x120 Size 60x120 lbs Per Sheet 60x120 lbs 60x120
Mat 2023.2 Thick 2023.2 Size 2023.2 lbs Per Sheet 2023.2 lbs 2023.2
Mat 0.41 Thick 0.41 Size 0.41 lbs Per Sheet 0.41 lbs 0.41
Excel 文件
11960120 0.119 60x120 250.00 [=13=].4100
18860120 0.188 60x120 383.00 [=13=].4100
25060120 0.250 60x120 510.00 [=13=].4100
25072144 0.250 72x144 734.40 [=13=].4100
31360120 0.313 60x120 633.26 [=13=].4100
37560120 0.375 60x120 758.70 [=13=].4100
50060120 0.500 60x120 1011.60 [=13=].4100
75060120 0.750 60x120 1517.40 [=13=].4100
10060120 1.000 60x120 2023.20 [=13=].4100
我想像阅读二维数组一样阅读上面的 excel 文件。例如:
array[0][0] = 11960120; array[0][1] = 0.119; array[0][2]= "60x120";
如果我可以像上面那样使用 getter 和 Items class 中的 setter 读取行和列,那么只需将 "Item" 放入地图中并用商品编号,例如 11960120 在上面 excel 示例中的单元格 [0][0] 中。
为了更清楚,我想像这样阅读 excel 文件
11960120 0.119 60x120 250.00 [=15=].4100
然后阅读下一行
18860120 0.188 60x120 383.00 [=16=].4100
每个单元格将被设置到项目中class。
您可以使用 Apache Metamodel 库来查询 Excel 电子表格。我在下面显示的方法保留了列名,因此您可以编写一个方法来按列名和行号获取值。如果您没有 want/have 列名称,则必须调整此代码:
private static org.slf4j.Logger logger = LoggerFactory.getLogger( "Data" );
public static Object[][] getExcelData( File excelFile, String sheetName )
{
ExcelConfiguration conf = new ExcelConfiguration( 1, true, false );
DataContext dataContext = DataContextFactory.createExcelDataContext( excelFile, conf );
DataSet dataSet = dataContext.query()
.from( sheetName )
.selectAll()
.where("run").eq("Y")
.execute();
List<Row> rows = dataSet.toRows();
Object[][] myArray = get2ArgArrayFromRows( rows );
return myArray;
}
/**
* Gets a 2D Object array from a List of Row objects that is only 2 args wide.
* @param rows
* @return
*/
public static Object[][] get2ArgArrayFromRows( List<Row> rows ) {
Object[][] myArray = new Object[rows.size()][2];
int i = 0;
SelectItem[] cols = rows.get(0).getSelectItems();
for ( Row r : rows ) {
Object[] data = r.getValues();
for ( int j = 0; j < cols.length; j++ ) {
if ( data[j] == null ) data[j] = ""; // force empty string where there are NULL values
}
myArray[i][0] = cols;
myArray[i][2] = data;
i++;
}
logger.info( "Row count: " + rows.size() );
logger.info( "Column names: " + Arrays.toString( cols ) );
return myArray;
}
然后,如果需要,可以使用 Google Guava 将二维数组转换为列表。 (我认为如果有欺骗,转换为 Map 会有问题吗?):
Double[][] array;
List<List<Double>> list = Lists.transform(Arrays.asList(array),
new Function<Double[], List<Double>>() {
@Override public List<Double> apply(Double[] row) {
return Arrays.asList(row);
}
}
}
您当前的循环:
for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
Row row = rit.next();
for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
Cell cell = cit.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
mat = cell.getStringCellValue();
thick = cell.getStringCellValue();
size = cell.getStringCellValue();
lbsPerSheet = cell.getStringCellValue();
lbs = cell.getStringCellValue();
System.out.println("Mat "+mat+" Thick "+ thick+" Size "+size+" lbs Per Sheet "+lbsPerSheet+" lbs "+lbs+ "\t");
}
System.out.println();
}
...基本上是说:"For each row and each cell in this sheet, put the value of that particular cell into these five different variables"。 cell
是当前单元格,获取它的字符串值并将其放入所有变量中实际上没有意义。您可能希望第一个单元格转到 mat
,第二个单元格转到 thick
等等。您可以执行以下两项操作之一:
不循环赋值:
for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) { Row row = rit.next(); Iterator<Cell> cit = row.cellIterator() Cell cell; if ( cit.hasNext() ) { cell = cit.next(); cell.setCellType(Cell.CELL_TYPE_STRING); mat = cell.getStringCellValue(); } if ( cit.hasNext() ) { cell = cit.next(); cell.setCellType(Cell.CELL_TYPE_STRING); thick = cell.getStringCellValue(); } if ( cit.hasNext() ) { cell = cit.next(); cell.setCellType(Cell.CELL_TYPE_STRING); size = cell.getStringCellValue(); } if ( cit.hasNext() ) { cell = cit.next(); cell.setCellType(Cell.CELL_TYPE_STRING); lbsPerSheet = cell.getStringCellValue(); } if ( cit.hasNext() ) { cell = cit.next(); cell.setCellType(Cell.CELL_TYPE_STRING); lbs = cell.getStringCellValue(); } System.out.println("Mat "+mat+" Thick "+ thick+" Size "+size+" lbs Per Sheet "+lbsPerSheet+" lbs "+lbs+ "\n"); }
使用数组并循环:
for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) { Row row = rit.next(); String[] cells = new String[row.getPhysicalNumberOfCells()]; int i = 0; for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) { Cell cell = cit.next(); cell.setCellType(Cell.CELL_TYPE_STRING); cells[i++] = cell.getStringCellValue(); } // At this point you can put the values of the cells in // your map entry. System.out.println(Arrays.toString(cells); }