TheGateKeeper
TheGateKeeper

Reputation: 4530

Opening .xlsx Files In Npoi

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.

Upvotes: 0

Views: 10177

Answers (5)

Simon
Simon

Reputation: 439

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... ;)

Upvotes: 5

David Robbins
David Robbins

Reputation: 10046

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();

Upvotes: 2

ssamayoa
ssamayoa

Reputation: 157

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 :(

Upvotes: 1

Tony Qu
Tony Qu

Reputation: 756

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

Upvotes: 0

TheGateKeeper
TheGateKeeper

Reputation: 4530

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.

Upvotes: 0

Related Questions