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.
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... ;)
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();
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 :(
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.
Please try the latest NPOI version: NPOI 2.0 RC. Here is the link: https://npoi.codeplex.com/releases/view/112932
Related
I'm writing a program to process some excel documents, the program is written in C# in Visual Studio 2010, and I'm using the NPOI library.
I notice the I was not able to use CloneSheet() for the xlsm file, but I was able to do so with xlsx.
CloneSheet() is a function that I really need for the process, so I really would like to get it working instead of copy everything cell by cell.
I thought of converting the file to xlsx. I was able to do it manually, but not programmatically.
This is the code I wrote to attempt to do so:
XSSFWorkbook workbook;
//read original xlsm file into workbook
using (FileStream file = new FileStream(#"OriginalFile\" + filename, FileMode.Open, FileAccess.Read))
{ workbook = new XSSFWorkbook(file); }
//change file extension to xlsx and save in a new location
filename = Path.ChangeExtension(filename, "xlsx");
if (!Directory.Exists("NewFile"))
Directory.CreateDirectory("NewFile");
FileStream stream = new FileStream(("NewFile\\New" + filename), FileMode.Create, System.IO.FileAccess.Write);
workbook.Write(stream);
stream.Close();
//read the newly created file from the new location
using (FileStream file = new FileStream(#"NewFile\\New" + filename, FileMode.Open, FileAccess.Read))
{ workbook = new XSSFWorkbook(file); }
The above code will create the new xlsx file, but the file cannot be open and seems to be corrupted...
I've been googling for a while and couldn't seem to find a solution, can anyone help or point me to the right direction?
--EDIT---
I tried a different method using Open XML I found here, but I have the same issue, where the file is created but I can't open the file.
However, the file doesn't seem to be corrupted. My program that is reading the file have no problem reading the data, and when I try to open the file in excel it's saying "The file is a macro-free file, but contains macro-enable content."
Seems like I'm getting closer, but I need to be able to open the output file otherwise it's no use....
You may have to roll your own method: iterate rows then cells. This post Copy Row Helper got me started. You'll have to modify it to use two workbooks. This method will copy data and formulas. CellStyles will have to be recreated as they they specific to the originating workbook.
I am trying to open a file, but I received:
The process cannot access the file because it is being used by another process. The File is an XML-Document. Can anyone help?
string activeDirectory = #"X:\SubGraph\";
string[] files = Directory.GetFiles(activeDirectory);
foreach (string fileName in files){
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
After using a file, you must to close it, I think:
foreach (string fileName in files)
{
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//your code
file.Close();
}
If you are using this piece of code in some kind of loop you need to close your FileStream each time before finishing loop cycle.
file.Close();
Or use "using" construction like this:
using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
// your code goes here
file.Close();
}
Moreover, you better to accustom yourself to close all manually created streams after they are unnecessary anymore.
Under some circumstances Windows locks the files. In your case can be:
Another process is locking the file. It might be windows or you av software or who knows.
In order to discover who is locking the file you might several tools like wholockme or Unlocker. These tools will tell you which process is locking the file and even allow you to unlock it.
Maybe you are locking your own file. In your code snippet seems you are not closing the file you are reading (Maybe you can edit your question and add all code). You should remember to include:
file.Close();
... or file will remain open.
I want to get only active sheet name from excel file using c#,but now i am getting all the sheet names,how can i get only active sheet name from excel using c#?
you can use the ActiveSheet property to get the name out of it
See link here http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._application.activesheet
e.g.
WorkSheet sheet = (WorkSheet)_application.activesheet;
string name = sheet.Name;
You may want to use NugetPackage Library NPOI (https://archive.codeplex.com/?p=npoi).
You can retrieve lots of useful information about Excel files (XLSX or XLS). Including the active worksheet when the workbook was saved (ActiveSheetIndex property).
It has the advantage of not relying on Microsoft Interpot API (not requiring Excel to be installed).
You may try this:
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
...
//Stream
FileStream arquivoXLSX = new FileStream(XLSXWorkbokPath, FileMode.Open, FileAccess.Read);
//Workbook Data from XML
XSSFWorkbook workbook = new XSSFWorkbook(XLSXWorkbokPath);
//Active Worksheet Index
var ActWsIndex = workbook.ActiveSheetIndex;
//Active Worksheet Name
var ActWsName = workbook.GetSheetName(ActWsIndex);
I hope it helps.
I am attempting to export data to a excel spreadsheet and so open that Excel spreadsheet using Visual Studio 2010 and Excel 2010 on Windows 7, but I get the following error.
'1.xls' cannot be accessed. The file may be corrupted, located on a server that is not responding, or read-only.
I have checked the file path and given Everyone full control of the file. Here is my code.
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "(Excel Files)|*.xls";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.Stream st = new System.IO.FileStream(saveFileDialog1.FileName,
FileMode.Create,
FileAccess.Write,
FileShare.None);
this.gridEXExporter1.ExportMode = Janus.Windows.GridEX.ExportMode.AllRows;
gridEXExporter1.Export(st);
//////////////////////////////////////
///// Opening the Excel File
/////////////////////////////////////
Process.Start(saveFileDialog1.FileName);
Thanks in advance.
You probably have to close the output file before you try to open it using Excel. Call st.Close() before Process.Start.
How could I archive this:
programmatically?
Is there a way of creating resource files with code?
I think you can't watch in resources but you can use resource.
FileStream fs = new FileStream("Resources",FileMode.OpenOrCreate, FileAccess.Write);
IResourceWriter writer = new ResourceWriter(fs);
writer.AddResource("TextFile1", "SomeText");
writer.Generate();
writer.Close();