簡體   English   中英

我試圖從文件中讀取字符串並將其存儲在Excel工作表中

[英]I tried to read string from file and store it in Excel sheet

這就是我嘗試過的

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)

國家清單

[]

嘗試改變

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM