我正在尝试从excel读取数据,并希望在Java中为标题和值创建Map。
我可以使用Java读取数据,但是我不想跳过空值,因为我需要创建标头和值的hashmap
,即使它是空的。
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
//System.out.println(cell.getCellType());
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
System.out.print(cell.getNumericCellValue() + ";");
}
if(cell.getCellType() == Cell.CELL_TYPE_STRING)
{
System.out.print(cell.getStringCellValue() + ";");
}
if(cell.getCellType() == Cell.CELL_TYPE_BLANK)
{
System.out.print("Blank");
}
}
System.out.println("");
}
file.close();
attr_item_code;tag_warranty;MASTER;1032839;MASTER
我要为上述数据创建一个hashmap
{
"attr_item_code":"1032839",
"tag_warranty":"",
"MASTER":"MASTER"
}
答案 0 :(得分:0)
尝试一下:
Iterator<Row> rowIterator = sheet.iterator();
boolean isHeader = true;
HashMap<String, ArrayList<String>> data = new HashMap<String, ArrayList<String>>();
HashMap<Integer, String> headers = new HashMap<Integer, String>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
int rowNum = 1;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (isHeader) {
data.put(String.valueOf(cell.getNumericCellValue()), new ArrayList<String>());
headers.put(rowNum, String.valueOf(cell.getNumericCellValue()));
} else {
ArrayList<String> tmp = data.get(headers.get(rowNum));
tmp.add(String.valueOf(cell.getNumericCellValue()));
data.put(headers.get(rowNum), tmp);
}
System.out.print(cell.getNumericCellValue() + ";");
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (isHeader) {
data.put(cell.getStringCellValue(), new ArrayList<String>());
headers.put(rowNum, cell.getStringCellValue());
} else {
ArrayList<String> tmp = data.get(headers.get(rowNum));
tmp.add(cell.getStringCellValue());
data.put(headers.get(rowNum), tmp);
}
} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
if (isHeader) {
data.put("", new ArrayList<String>());
headers.put(rowNum, "");
} else {
ArrayList<String> tmp = data.get(headers.get(rowNum));
tmp.add("");
data.put(headers.get(rowNum), tmp);
}
}
rowNum++;
}
if (isHeader) {
isHeader = false;
}
}
file.close();
答案 1 :(得分:0)
如果column1或column2中有空行,则(cellIterator.hasNext())将跳过空单元格。您最终将毫无头绪。因此,您已经更改了读取文件的方式。
您最好使用列索引读取excel文件。
示例代码(未编译)。
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row != null) {
// 1st cell
Cell cell1 = row.getCell(1);
// 2nd cell
Cell cell2 = row.getCell(2);
// 3rd cell
Cell cell3 = row.getCell(3);
...... so on
// you know the cell type so read accordingly
// cell1.getNumericCellValue() or cell1.getStringCellValue() etc
}
}
答案 2 :(得分:0)
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ReadExcelDemo1
{
public static void main(String[] args)
{
try
{
String header="";
FileInputStream file = new FileInputStream(new File("C:/Talend/Testing/master_old.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(0);
Iterator<Row> rowIterator = sheet.iterator();
boolean isHeader = true;
//HashMap<String,Object> masterMap = new HashMap<String,Object>();
List<Object> masterList = new ArrayList<Object>();
HashMap<String, ArrayList<String>> masterData = new HashMap<String, ArrayList<String>>();
HashMap<Integer, String> headers = new HashMap<Integer, String>();
//int rowStart = sheet.getFirstRowNum();
//int rowEnd = sheet.getLastRowNum();
int rowStart = sheet.getFirstRowNum();
int rowEnd = sheet.getPhysicalNumberOfRows();
//System.out.println(sheet.getFirstRowNum() + " - ---------------"+sheet.getLastRowNum());
for (int rowNum = rowStart; rowNum <= rowStart; rowNum++)
{
Row r = sheet.getRow(rowNum);
int lastColumn = r.getLastCellNum();
for (int cn = 0; cn < lastColumn; cn++)
{
Cell cell = r.getCell(cn);
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
int value = (int) Math.round(cell.getNumericCellValue());
masterData.put(String.valueOf(value), new ArrayList<String>());
headers.put(cn, String.valueOf(value));
}
else if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
masterData.put(String.valueOf(cell.getStringCellValue()), new ArrayList<String>());
headers.put(cn, String.valueOf(cell.getStringCellValue()));
}
}
}
for (int rowNum = rowStart + 1; rowNum <= rowEnd; rowNum++)
{
HashMap<String, Object> data = new HashMap<String, Object>();
Row r = sheet.getRow(rowNum);
if (r == null)
{
// This whole row is empty
// Handle it as needed
continue;
}
int lastColumn = r.getLastCellNum();
for (int cn = 0; cn < lastColumn; cn++)
{
//Cell cell = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
Cell cell = r.getCell(cn);
if(cell != null)
{
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
String headerValue = headers.get(cn);
List<String> tmp = new ArrayList<>();
int value = (int) Math.round(cell.getNumericCellValue());
tmp.add(String.valueOf(value));
data.put(headerValue, tmp);
}
else if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
String headerValue = headers.get(cn);
List<String> tmp = new ArrayList<>();
String strValue = String.valueOf(cell.getStringCellValue());
if(headerValue.startsWith("taxo_") || headerValue.startsWith("tag_") || headerValue.equals("MASTER"))
{
data.put(headerValue, Arrays.asList(strValue.split(",")));
}
else
{
tmp.add(String.valueOf(cell.getStringCellValue()));
data.put(headerValue, tmp);
}
}
else if (cell.getCellType() == Cell.CELL_TYPE_BLANK)
{
String headerValue = headers.get(cn);
List<String> tmp = new ArrayList<>();
data.put(headerValue, tmp);
}
}
else
{
String headerValue = headers.get(cn);
List<String> tmp = new ArrayList<>();
data.put(headerValue, tmp);
}
}
masterList.add(data);
}
System.out.println(new ObjectMapper().writeValueAsString(masterList));
//System.out.println(new ObjectMapper().writeValueAsString(headers));
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}