这是我试过的
public class UniqueUSER
{
static HSSFWorkbook hwb=new HSSFWorkbook();
static HSSFSheet sheet = hwb.createSheet("new sheet");
public static void main(String[]args) throws IOException
{
HSSFRow row;
HashSet<String> names = new HashSet<>();
BufferedReader br = new BufferedReader (new FileReader("Sample.log"));
PrintStream out=new PrintStream("D:/Excel.xls");
String str=null;
while((str=br.readLine())!=null)
{
if(str.contains("FLTR"))
{
String user=str.substring(97, 135);
names.add(user);
HSSFRow row1 = sheet.createRow((short)count);
}
}
Iterator itr=names.iterator();
while(itr.hasNext())
{
out.println(itr.next());
}
}
}
此程序将值存储到Excel工作表但是当我使用以下程序读取相同文件时,我收到异常和错误..
public class Country1
{
private String name;
private String shortCode;
public Country1(String n, String c)
{
this.name=n;
this.shortCode=c;
}
public void Country(String name2, String shortCode2)
{
// TODO Auto-generated constructor stub
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getShortCode()
{
return shortCode;
}
public void setShortCode(String shortCode)
{
this.shortCode = shortCode;
}
@Override
public String toString()
{
return name + "::" + shortCode;
}
}
public class ReadExcel
{
public static List<Country1> readExcelData(String fileName)
{
List<Country1> countriesList = new ArrayList<Country1>();
try
{
//Create the input stream from the xlsx/xls file
FileInputStream fis = new FileInputStream(fileName);
//Create Workbook instance for xlsx/xls file input stream
Workbook workbook = null;
if(fileName.toLowerCase().endsWith("xlsx"))
{
workbook = new XSSFWorkbook(fis);
}
else if(fileName.toLowerCase().endsWith("xls"))
{
workbook = new HSSFWorkbook(fis);
}
//Get the number of sheets in the xlsx file
int numberOfSheets = workbook.getNumberOfSheets();
//loop through each of the sheets
for(int i=0; i < numberOfSheets; i++)
{
//Get the nth sheet from the workbook
Sheet sheet = workbook.getSheetAt(i);
//every sheet has rows, iterate over them
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
String name = "";
String shortCode = "";
//Get the row object
Row row = rowIterator.next();
//Every row has columns, get the column iterator and iterate over them
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
//Get the Cell object
Cell cell = cellIterator.next();
//check the cell type and process accordingly
switch(cell.getCellType())
{
case Cell.CELL_TYPE_STRING:
if(shortCode.equalsIgnoreCase(""))
{
shortCode = cell.getStringCellValue().trim();
}
else if(name.equalsIgnoreCase(""))
{
//2nd column
name = cell.getStringCellValue().trim();
}
else
{
//random data, leave it
System.out.println("Randomdata::"+cell.getStringCellValue());
}
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println("Random data::"+cell.getNumericCellValue());
}
} //end of cell iterator
Country1 c = new Country1(name, shortCode);
countriesList.add(c);
} //end of rows iterator
} //end of sheets for loop
//close file input stream
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return countriesList;
}
public static void main(String args[])
{
List<Country1> list = readExcelData("D:\\Excel.xls");
System.out.println("Country List\n"+list);
}
}
我很困惑哪个程序是正确的,哪个是错的..请有人帮我...谢谢很多
例外情况如下:
Unable to read entire header; 320 bytes read; expected 512 bytes
at org.apache.poi.poifs.storage.HeaderBlock.alertShortRead(HeaderBlock.java:227)
at org.apache.poi.poifs.storage.HeaderBlock.readFirst512(HeaderBlock.java:208)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:128)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:342)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:323)
at com.unisys.ReadExcel.readExcelData(ReadExcel.java:32)
at com.unisys.ReadExcel.main(ReadExcel.java:97)
国家/地区列表
[]
答案 0 :(得分:0)
尝试改变
Iterator<Row> rowIterator = sheet.iterator();
到
Iterator<Row> rowIterator = sheet.rowIterator()
此外,在编写创建行时,但您没有向它们写任何内容
HSSFRow row1 = sheet.createRow((short)count);
然后创建单元格
row.createCell (0).setCellValue (user);
更新
再看看你的代码,你为什么要这样做
PrintStream out=new PrintStream("D:/Excel.xls");
使用POI API创建xls。