Quantcast
Channel: npoi Discussions Rss Feed
Viewing all 637 articles
Browse latest View live

New Post: EOF In Header exception when reading .xlsm

$
0
0
FYI :

this code works in order to read a file into a XSSFWorkbook:

private IWorkbook InitializeAddingWorkbook(HttpPostedFile postedFile)
 {
    Stream fileStream = postedFile.InputStream;

    byte[] fileContents = new byte[postedFile.ContentLength];
    fileStream.Read(fileContents, 0, postedFile.ContentLength);
    fileStream.Position = 0; // <---- remember to rewind the stream

    return new XSSFWorkbook(fileStream);

}
Just remember that XSSFWorkbook needs a zip library to decode zipped xml, and the stream passed to the unzipper needs to be at 0 position.
When fileStream.Read( ) reads, the stream keeps positioned at end, so when unzipper needs to read it gets "EOF".

Thanks for your help.

Luis.

New Post: EOF In Header exception when reading .xlsm

$
0
0
That need to rewind seems sort of defect-ish...I wonder if it makes sense in this project to reset the position of the stream during object initialization...in any event, hopefully this message thread will help someone someday. Good stuff to know.

New Post: [NPOI.XSSF.UserModel] XLSX file : repair needed when using Double in SetCellValue()

$
0
0
Hello,

Sorry for the long wait, I was stuck on another big project and could not make the test.

Here what I did :
1- When instanciating the new XSSFWorkbook, I added this command :
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
2- In order to keep the application "clean", after writing the final file to the disk, I added this command:
Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture
Everything is working alright and there is no more file corruption

Thanks for the support, I hope my thread will help some other people :)

New Post: Error while reading .doc file

$
0
0
Hi!

I got this file when i'm trying to read .doc (97-2003) file.

An unhandled exception of type 'ICSharpCode.SharpZipLib.Zip.ZipException' occurred in ICSharpCode.SharpZipLib.dll

Additional information: Wrong Local header signature: 0xE011CFD0

This is my code:

FileStream fs = new FileStream("test.doc", FileMode.Open, FileAccess.Read);
XWPFDocument document = new XWPFDocument(fs);
XWPFWordExtractor exctr = new XWPFWordExtractor(document);
MessageBox.Show(exctr.Text);

And this is my doc file:
http://speedy.sh/a5e7G/word.doc

New Post: Error reading .xlsm file with pivotTable

$
0
0
I have a template .xlsm file with two pivotTable in sheet2 and sheet3. If I read the file with NPOI, and save it to another path(without any changes), those pivotTable cannot be reopened again. To be specific, when I opened the new file, it will report crash and begin auto fixing. After the fixing, only some string left in sheet2 and sheet3, with no table left.

The code is here:
        FileStream readFile = new FileStream(templatePath, FileMode.Open, FileAccess.Read);

        XSSFWorkbook xssfworkbook = new XSSFWorkbook(readFile);

        readFile.Close();

        FileStream writeFile = new FileStream(outputPath, FileMode.Create, FileAccess.Write);

        xssfworkbook.Write(writeFile);

        writeFile.Close();
You can see I didn't make any changes to the template, but those pivotTable will still be disappeared in the new file.

New Post: Stress test of XSSF proves exponential time (performance concerns)

$
0
0
Why not keep the rows as an enumerated list and then have a render method that does the xml serialization at the end. That should increase performance handsomely.

New Post: How can I change the long date format to short date format?

$
0
0
when I export all data to excel file, how can I change the long date format to short date format?

i.e., exported date format is 4/23/2013 12:00:00 AM, good format is 4/23/2013

Code as below:
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("Sheet1");
        HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);

        // handling header.
        foreach (DataColumn column in srcTable.Columns)
            headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);

        // handling value.
        int rowIndex = 1;
        foreach (DataRow row in srcTable.Rows)
        {
            HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
            foreach (DataColumn column in srcTable.Columns)
            {
                dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
            }
            rowIndex++;
        }
Your help was greately appreciated.

New Post: NPOI 2.0 Source Code

$
0
0
I could not find the NPOI 2.0 source code, someone could pass me a link where to download it?
Thanks

New Post: Read xlsx file

$
0
0
Hi

I am a newbie. I would like a simple example of how to read a number value from a cell.

Thanks
Herman

New Post: To write xlsx to a stream

$
0
0
Hi, when I tried to write a xlsx file to a memoryStream using XSSFWorkbook.write, I found the method closed the stream. This behavior made the stream cannot be accessed. HSSFWorkbook.write did not do this behavior. Please check it. Thanks!

New Post: How can I change the long date format to short date format?

$
0
0
I have the same question. Has anyone forced the Excel export to format the date without the time?

I know that it's possible by casting the date as a string, but this solution is not ideal because it results in improper sorting.

New Post: How to change the Numberformat to Text from C#

$
0
0
Hi,

I am using NPOI for Export to .xls file. After Export to .xls when I open all the cell should be in "Text" Numberformat instead of "General". All the cells in the .xls file should take example if I enter 0098 it should stay it shouldn't change to 98.

Please let me know how can I set this in C# using NPOI.

Thanks....

New Post: Excel Conversion to PDF using NPOI

$
0
0
Hi ,

i want to convert the excel workbook which has images, chart and more than 200 columns and 1000 rows. Can anyone please provide the sample code to get it done . i have tried the solutions but images and tables are not getting converted properly. it converts only the text and produce the PDF documents.

New Post: New Version On NuGet

$
0
0
Anyway to publish a NuGet package for version 2.0 beta 1 as a Pre-Release Package?

New Post: How to change the Numberformat to Text from C#

$
0
0
I dont know if this is what you're looking for, but you can set the Format of cells to Text like this:
//quick example
var cell = sheet.GetRow(3).GetCell(2);
cell.CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
you can do the same thing at the Row level if you like.

Hope it helps

New Post: CustomProperties in Excel (.xls): issue name and value in unicode, should be UTF-8

$
0
0
I want to add name-value pairs in the CustomProperties of an 97-2003 (.xls) Excel spreadsheet. Adding works, but name and value are inserted as unicode. The name is not not readable anymore (look like chinese characters).
Name and value pair should be UTF-8 (single byte) for correct use in Excel 97-2003.
Does anyone have experience with this?

New Post: How can I change the long date format to short date format?

$
0
0
This is working well for me, using your code I added date formatting. The date is stored in Excel as the dateserial value so should behave correctly when sorting rather than pretending to be a date.
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("Sheet1");
            HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);

            // handling header.
            foreach (DataColumn column in srcTable.Columns)
                headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);

            //Create date format    
            ICellStyle cellDateStyle = workbook.CreateCellStyle();
            cellDateStyle.DataFormat = workbook.CreateDataFormat().GetFormat("dd/mm/yyyy");

            // handling value.
            int rowIndex = 1;
            foreach (DataRow row in srcTable.Rows)
            {
                HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
                foreach (DataColumn column in srcTable.Columns)
                {
                    if (!System.DBNull.Value.Equals(row[column]) && column.DataType == typeof(DateTime))
                    {
                        dataRow.CreateCell(column.Ordinal).SetCellValue(Convert.ToDateTime(row[column]));
                        dataRow.GetCell(column.Ordinal).CellStyle = cellDateStyle;
                    }
                    else
                        dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                }
                rowIndex++;
            }

New Post: XLS file created from template doesn't save correctly - any ideas?

$
0
0
Hi,

Using NPOI, I'm successfully creating a spreadsheet from an XLT template, but when I subsequently open the spreadsheet in MS Excel it still thinks it's an XLT template.

I've checked through the online documentation and my code appears to be correct - does anyone have any ideas on what I'm doing wrong please?

Steps to re-produce are as follows:

1) Create XLT template (using MS Excel)
2) Use the following code to create a spreadsheet from the template
const string templatePath = @"C:\Temp\Template.xlt";
const string spreadsheetPath = @"C:\Temp\Spreadsheet.xls";
HSSFWorkbook workbook;
using (var fs = new FileStream(templatePath, FileMode.Open))
{
                workbook = new HSSFWorkbook(fs);
}
using (var fs = new FileStream(spreadsheetPath, FileMode.Create))
{
                workbook.Write(fs);
}
3) Open the file in MS Excel and click File > Save As

At this point MS Excel thinks you are trying to save an XLT template file (it automatically selects to save as type xlt file).

If anyone has any ideas on what I might be doing wrong, or if this is a genuine bug, that would really helpful!

If it has any bearing, I'm using NPOI v2.0.1.0 and Excel 2003.

Thanks.

New Post: HSSFColor

$
0
0
Slowpokes attacking! It was really hard find solution for XSSFColor, but I got it (think HSSFColor same):
style.FillForegroundXSSFColor.SetRgb(new byte[] { Color.LightPink.R, Color.LightPink.G, Color.LightPink.B });

New Post: NPOI 2.0 Source Code

Viewing all 637 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>