17

I'm using excelLibrary to programatically create excel files but I get a file format error when I try to open the generated files in Microsoft Office Excel.

I've seen this has been reported but there's still no answer about it.

I use Office 2010 and I'm able to open any other .xls (97-2003 file format) but the ones generated with excelLibrary. I've also tried Open Office and still can't open the generated file. I haven't tried to open them in Office 97-2003.

Just try the sample code to reproduce the error.

Have anybody found how to use the library and not run into this problem?

mmutilva
  • 18,688
  • 22
  • 59
  • 82
  • Not sure about the error however the library doesnt have 2007 / 2010 support, perhaps look at something newer? http://netoffice.codeplex.com/ – Kane Nov 12 '11 at 21:22
  • I've found a solution, see my new answer. – aleroot Nov 14 '11 at 20:33
  • Try [EPPlus](http://epplus.codeplex.com). It requireds .NET 3.5 but is awesome and very powerful. It uses OpenOfficeXml. – Leslie Godwin Jul 01 '15 at 05:29

3 Answers3

52

Found a solution :

string filename = "c:\Test.xls";
Workbook workbook = new Workbook();
Worksheet sheet = new Worksheet("Test")
workbook.Worksheets.Add(sheet)

for(int i = 0;i < 100; i++)
      sheet.Cells[i,0] = new Cell("");

workbook.save(filename);

The problem is that Office 2010 doesn't support it unless there are 100 or more Cells Filled.

My work around was to have it fill 100 cells in a for loop with "". That way it gets it's 100 cell count in and then it works just fine.

Reference : here

Mohammadreza
  • 3,139
  • 8
  • 35
  • 56
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • That worked! But the 256 column limit is just too restrictive (Excel 97?) Too bad, I really prefer LGPL license over Apache. Trying out NPOI below. – user922020 Mar 11 '15 at 22:09
  • you are my hero – D__ May 22 '20 at 15:03
  • This is very strange. Why does Office 2010 requires at least 100 cells in a .xls file? Can a .xls file with less than 100 cells be opened in earlier versions of Office, like Office 2003? – zhoudu May 31 '23 at 03:37
5

Unfortunately excel file exported with excelLibrary are not compatible with office 2010 Excel, this is an already reported issue but seems that the library development is no longer active .

I've switched to NPOI .

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • +1 for NPOI, just be aware that NPOI API is pretty verbose in style, requiring you to constantly Create* stuff, in particular when it comes to formatting. Also, their name abbreviations are truly awful, making intellisense nearly useless until you remember that the thing you want is called "HSSF". – Roman Starkov Jul 30 '14 at 18:19
  • 1
    Yes. Try [EPPlus](http://epplus.codeplex.com). It requireds .NET 3.5 but is awesome and very powerful. It uses OpenOfficeXml. – Leslie Godwin Jul 01 '15 at 05:28
-1

Since the sheet name is not given properly, it was throwing that error.

Once we give the sheet a name, it will work properly.

Robert
  • 5,278
  • 43
  • 65
  • 115