I have an excel file contains Attachment column, which has another excel object embedded into it.
I need to read that embedded excel file and process its data.
So far i used Excel Reader to read the normal excel file and retrieve as DataSet.
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
// Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
excelReader.Close();
Now i want to read the embedded object(excel file)
Is there any dll/component/code which will help us to read embedded object inside excel file?
Note: I dont have a Microsoft Office Package installed in server. So i dont want to use Microsoft.Interoperability package
Related
I want to open Excel from byte[] because my file is encrypted and I want to open after decrypt but without write in a file.
The office has "restricted access" and I want to open my file with this protection but without saving the decrypted content in a file.
myApp.Workbooks.Open only supports a path.
Is it possible?
As an alternative to OpenXml there's also ExcelDataReader which from my experience is a lot faster in processing data compared to Interop.Excel(around 3 times+).
It can also open encrypted Excel files directly(stackoverflow)
The github page for ExcelDataReader has some great examples on how to use it. The only thing you'd have to do is:
This:
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
Becomes this:
using (var stream = new MemoryStream(yourByte[])
And if you just want to open the password protected excel file you'd do this:
var conf = new ExcelReaderConfiguration { Password = "yourPassword" }; //Add this
excelReader = ExcelReaderFactory.CreateReader(stream, conf); //change the excel Reader to this
Make sure to check the Github page for more info!
It is not possible because the interop is actually an interface for programs to run and operate existing excel on the computer.
I think you need to use openxml created by Microsoft to work with excel word and PowerPoint.
DocumentFormat.OpenXml
Then you can use:
ExcelPackage excelPackage = new ExcelPackage(stream)
or
var pck = new OfficeOpenXml.ExcelPackage();
pck.Load(File.OpenRead(path));
pck.Load(Stream) can use any stream as input not only from a file.
It depends on your needs.
I already created a docx file by aspose.word and I need to embed an worksheet to this file thus I created a workbook and worksheet .
Document doc = new Document();
Workbook workbook = new Workbook();
int i = workbook.Worksheets.Add();
Worksheet sheet = workbook.Worksheets[i];
and after filling cells in the sheet I save my excel file in storage and embedded it to my docx by using insertOleObject and pass file address to this method by using file stream.
workbook.Save(dir+"output.xlx");
Stream memStream = File.OpenRead(dir+"output.xlx");
Shape oleObject = builder.InsertOleObject(memStream, "Excel.Sheet.2",false,null);
but I want to embed a worksheet(or workbook) directly without using workbook.Save method and Stream object.
As bassfader shared, first save your Workbook to Memory Stream. Besides, you can convert your Memory Stream into Byte Array. Then you can use the same code as you have shown to insert Workbook (which is now in form of memory stream or byte array) as Ole Object in your MS-Word Document.
Note: I am working as Developer Evangelist at Aspose
I am importing a large set of Excel data (about 150k rows and 115 columns) into SQL Server.
using IExcelDataReader
Dim stream As FileStream = File.Open(filePath, FileMode.Open, FileAccess.Read)
' Reading from a binary Excel file ('97-2003 format; *.xls)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateBinaryReader(stream)
' Reading from a OpenXml Excel file (2007 format; *.xlsx)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
result.Tables
Dim result As DataSet = excelReader.AsDataSet()
excelReader.IsFirstRowAsColumnNames = True
Dim result As DataSet = excelReader.AsDataSet()
' Do a Bulk copy Here
excelReader.Close()
After waiting 2-3 minutes, I got a System.OutOfMemoryException
Is there a way to get rid of this exception and import the data faster?
I tried the ACE.OLEDB.12.0 provider but it is not working on iis .
You should use the excelReader.Read() method to iterate through each row, instead of returning the entire spreadsheet in a single call, which is what excelReader.AsDataSet() is doing (which is then causing you to run out of memory).
See here for details on how to use a reader (although in that example it's a SqlDataReader, the logic should be the same).
The documentation for the ExcelDataReader here (scroll down) has a vague example which matches the SqlDataReader docs.
Having a difficult time with this one...
Everything worked 100% with XLS (I'm using NPOI on my ASP.NET app):
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
private MemoryStream ms = new MemoryStream();
private IWorkbook workbook;
private ISheet worksheet;
private byte[] buffer;
using (FileStream file = new FileStream(#"C:\template.xls", FileMode.Open, FileAccess.Read))
{
workbook = new HSSFWorkbook(file);
}
worksheet = workbook.GetSheetAt(0);
worksheet.GetRow(11).GetCell(11).SetCellValue("hello"); // etc etc etc
workbook.Write(ms);
buffer = ms.ToArray();
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "application/vnd.ms-excel");
response.AddHeader("Content-Disposition", String.Format("attachment; filename=template.xls; size={0}", buffer.Length.ToString()));
response.BinaryWrite(buffer);
response.End();
Requirements have changed and the new Excel template uses features from XLSX, so it's now template.xlsx. I've been using NPOI 2.0 all along, and I've seen that it supports XLSX (using XSSF instead of HSSF). I changed the code like this (only showing the differences here - everything else is the same):
using NPOI.XSSF.UserModel;
using (FileStream file = new FileStream(#"C:\template.xlsx", FileMode.Open, FileAccess.Read))
{
workbook = new XSSFWorkbook(file);
}
response.AddHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.AddHeader("Content-Disposition", String.Format("attachment; filename=template.xlsx; size={0}", buffer.Length.ToString()));
The exception gets thrown at the line workbook.Write(ms);:
System.NotImplementedException: The method or operation is not implemented.
at NPOI.XSSF.UserModel.XSSFChartSheet.Write(Stream out1)
at NPOI.XSSF.UserModel.XSSFSheet.Commit()
at NPOI.POIXMLDocumentPart.OnSave(List`1 alreadySaved)
at NPOI.POIXMLDocumentPart.OnSave(List`1 alreadySaved)
at NPOI.POIXMLDocument.Write(Stream stream)
What could I be doing incorrectly?
edit: I've tried NPOI 2.1.1 Beta - same issue
I ran some tests with your sample code and a bunch of different XLSX files. Based on the results, and the exception you got, the issue is not with your code, but with the NPOI library and the XLSX template file you are working with.
Basically, in Excel, you can display a chart in two ways:
Embedded in a worksheet, such that the worksheet can display other information.
All by itself in a separate sheet. This is known as a Chart Sheet.
It seems that your XLSX template contains a chart sheet, and the error occurs while trying to write out the chart sheet (XSSFChartSheet.Write). Currently, NPOI has no support for writing out XLSX chart sheets, it just throws an exception instead.
This leaves you with three options:
Remove all chart sheets from your XLSX file.
Convert all chart sheets in your template to embedded charts. Note that in my testing, NPOI support for embedded charts was a bit iffy. While it did not throw an exception while writing them out, the resultant charts would sometimes have data labels added where there were none, or cause Excel to recover the document by removing the chart itself. Your mileage may vary.
Drop NPOI and use some other library, for e.g., EPPlus which seems to have support for charts in XLSX files, based on this answer.
I have an Excel Template which i want to write into with data from a database. Whenever I edit and Save the file in c#, when I open the template, Microsoft Office Excel says the file is corrupt. Apparently, I think I'm going about editing it wrongly. this is how i went about it below. I am using NPOI 2.0 beta 2. if it matters, the template contains macros and formulas
FIleStream fs = new FileStream(pathString, FIleMode.Open, FileAccess.Read);
IWorkbook wkb = WorkbookFactory.Create(fs);
ISheet sheet = wkb.GetSheet("sheet1");
ICell cell = sheet.GetRow(row).GetCell(column);
if(cell != null)
{
cell.SetCellValue(value);
}
FileStream fs1 = new FileStream(pathString, FileMode.OpenOrCreate);
wkb.Write(fs1);
fs.CLose();
fs1.Close();
But If I try to read the corrupted excel file, i can still retrieve values from the sheet using NPOI. Any pointers as to my errors. Thanks in anticipoation