我的目标是阅读具有已知格式的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 $0.4100
18860120 0.188 60x120 383.00 $0.4100
25060120 0.250 60x120 510.00 $0.4100
25072144 0.250 72x144 734.40 $0.4100
31360120 0.313 60x120 633.26 $0.4100
37560120 0.375 60x120 758.70 $0.4100
50060120 0.500 60x120 1011.60 $0.4100
75060120 0.750 60x120 1517.40 $0.4100
10060120 1.000 60x120 2023.20 $0.4100
我想阅读上面的excel文件,就像二维数组一样。例如:
array[0][0] = 11960120; array[0][1] = 0.119; array[0][2]= "60x120";
如果我可以像上面那样读取行和列,请使用Items类中的getter和setter,然后只需将“Item”放入一个地图中并用项目编号包装,例如11960120在单元格[0]中[0]在上面的excel例子中。
为了更清楚,我想像这样阅读excel文件
11960120 0.119 60x120 250.00 $0.4100
然后阅读下一行
18860120 0.188 60x120 383.00 $0.4100
每个单元格都将设置为Items类。
答案 0 :(得分:1)
您可以使用Apache Metamodel库查询Excel电子表格。我在下面显示的方法保留了列名,因此您可以编写一个方法来按列名和行号获取值。如果您不想/拥有列名,则必须调整此代码:
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将2-D数组转换为List。 (如果有傻瓜,我认为转换成地图会有问题吗?):
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);
}
}
}
答案 1 :(得分: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();
}
...基本上是这样说:“对于本表中的每一行和每个单元格,将该特定单元格的值放入这五个不同的变量中”。 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);
}