0

I am trying to open an .xlsx file using Npoi but it keeps crashing with the following error:

1 is not a supported code page.
Parameter name: codepage

My code is very simple:

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel Workbook|*.xlsx";

DialogResult dr = ofd.ShowDialog();

if (dr == DialogResult.OK)
{
    XSSFWorkbook myWorkbook;

    FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);

    using (fs)
    {
        myWorkbook = new XSSFWorkbook(ofd.FileName);
    }
}

The error happens while trying to create the workbook. I tried also using the stream, such as:

myWorkbook = new XSSFWorkbook(fs);

Does anyone know what is wrong? I can't find a proper example on the net for dealing with .xlsx files. I am suing the latest build (2.0.1).

Thanks.

Ben T
  • 4,656
  • 3
  • 22
  • 22
TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101

5 Answers5

5
ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = Encoding.Default.CodePage;
...
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
XSSFWorkbook workbook = new XSSFWorkbook(fs);

it works for me... ;)

Simon
  • 399
  • 1
  • 3
  • 10
2

I have been using the Workbook Factory without issues. It will detect whether the file is xls or xlsx and return the appropriate object for you. Note that this is version 2.06.

A quick sample:

_fileStream = new FileStream(filenamePath, FileMode.Open, FileAccess.Read);
_currentWorksheet = _workbook.GetSheetAt(0);
_workbook = WorkbookFactory.Create(_fileStream);
_fileStream.Close();
David Robbins
  • 9,996
  • 7
  • 51
  • 82
1

I have been Apache POI user for the last decade and I thought that NPOI was as good as his Java father but I'm afraid TheGateKeeper is right: a long way to go.

I have to look for OpenXML :(

ssamayoa
  • 157
  • 1
  • 5
0

I was able to open the file successfully using EPPlus, another Excel library. I still use NPOI for .xls files but for .xlsx I think it has a long way to go.

TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101
0

Please try the latest NPOI version: NPOI 2.0 RC. Here is the link: https://npoi.codeplex.com/releases/view/112932

Tony Qu
  • 676
  • 8
  • 14