Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there is Limition of using opencsv API or Apache Poi Api?

I am creating Excel file on the basis of CSV file.for reading CSV file,i am using Opencsv API and Apache POI.In my csv contain 65537 row.

class Test {
public static void main(String[] args) throws IOException {
    Workbook wb = new HSSFWorkbook();
    CreationHelper helper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("new sheet");

    CSVReader reader = new CSVReader(new FileReader("SampleData.csv"));
    String[] line;
    int r = 0;int count=0;
    while ((line = reader.readNext()) != null) {
        Row row = sheet.createRow((short) r++);
        count=count+1;
         System.out.println("count-"+count);
        for (int i = 0; i < line.length; i++)
            row.createCell(i)
               .setCellValue(helper.createRichTextString(line[i]));
    }

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
}}

when i run this program it give me following error:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid row number (-32768) outside allowable range (0..65535)at org.apache.poi.hssf.usermodel.HSSFRow.setRowNum(HSSFRow.java:232)
    at org.apache.poi.hssf.usermodel.HSSFRow.<init>(HSSFRow.java:86)
    at org.apache.poi.hssf.usermodel.HSSFRow.<init>(HSSFRow.java:70)
    at org.apache.poi.hssf.usermodel.HSSFSheet.createRow(HSSFSheet.java:205)
    at org.apache.poi.hssf.usermodel.HSSFSheet.createRow(HSSFSheet.java:71)
    at com.arosys.utilityclasses.Test.main(Test.java:23)Java Result: 1

I tried to trace how much row it support i found it only support 32768 and also tried for less number of row,it works nicely and create excel file.

please help me to sort out this problem,if my csv contain 65536 row then how i am bale to write excel file(Xls).

Thanks

like image 846
Sameek Mishra Avatar asked Jun 30 '11 15:06

Sameek Mishra


People also ask

What is the difference between HSSF & XSSF workbook?

HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (. xlsx) file format. HSSF and XSSF provides ways to read spreadsheets create, modify, read and write XLS spreadsheets.

Can Apache POI be compiled used with Java 11?

23. Can Apache POI be compiled/used with Java 11? Including the existing binaries on Java 11 as normal jar-files should work when using recent versions of Apache POI.

Can we use Apache POI for CSV files?

Apache POI was never designed to call on CSV files. While a CSV File may be opened in Excel, Excel has its own reader that does an auto import. This is assuming that your CSV has the . csv instead of the .


2 Answers

Why are you casting the row num to short in the following line

 Row row = sheet.createRow((short) r++);

Leave it as int

like image 58
aldrin Avatar answered Sep 24 '22 20:09

aldrin


Looks like it's your short value. the max value on a short is 32767 and you're trying to access one more than that.

like image 26
Michael J. Lee Avatar answered Sep 24 '22 20:09

Michael J. Lee