Quantcast
Channel: NPOI
Viewing all 1621 articles
Browse latest View live

Commented Unassigned: NPOI 2.1.1 HSSF SetDefaultColumnStyle changes cell color styles in whole sheet [13138]

$
0
0
Hello.
I'm new to NPOI and if I misunderstand something please correct me. I want to write a test application which opens an existing Excel Workbook (created by Excel 2007 or 2010 in 97-2003 xls Format) and removes the column style of column index 4. The first sheet in the Workbook includes several columns formatted in different value formats like Date and Text and different cell colors.
The only function I've found to set a column style was SetDefaultColumnStyle of ISheet.
Here is the code:
```
IWorkbook wb;
ISheet ws;
using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table.xls", FileMode.Open, FileAccess.Read))
{
wb = new HSSFWorkbook(file);
}
ws = wb.GetSheetAt(0);
ws.SetDefaultColumnStyle(4, wb.CreateCellStyle());

using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table_result.xls", FileMode.Create, FileAccess.Write))
{
wb.Write(file);
}
```
For the column E the call of SetDefaultColumnStyle works as expected. But the Colors of the cells in the other columns change. When I compare the original file with the file created by NPOI some of the cell colors are changed.
The description of SetDefaultColumnStyle says "Sets the default column style for a given column. POI will only apply this style to new cells Added to the sheet.". So this function should have no influence on any cell which already has its own cell style, didn't it? From my Point of view is a bug.
To proof that the color change is not a General Problem I also opened the workbook and saved it without changes. The resulting file is smaller than the original Excel file but the content and the formatting keeps the same. So, the Color change of the cells must have something to do with the call to SetDefaultColumnStyle.
It would be great if someone could tell me if there is another possibility to remove the default column style from a column, which does not affect the existing cell styles of the sheet. Furthermore, I hope that this behavior can be fixed in the next stable release of NPOI.
My testing workbook is attatched.

Best regards
Herbie
Comments: ** Comment from web user: tonyqus **

Looks to be a bug. I'll check.


Commented Unassigned: NPOI 2.1.1 HSSF SetDefaultColumnStyle changes cell color styles in whole sheet [13138]

$
0
0
Hello.
I'm new to NPOI and if I misunderstand something please correct me. I want to write a test application which opens an existing Excel Workbook (created by Excel 2007 or 2010 in 97-2003 xls Format) and removes the column style of column index 4. The first sheet in the Workbook includes several columns formatted in different value formats like Date and Text and different cell colors.
The only function I've found to set a column style was SetDefaultColumnStyle of ISheet.
Here is the code:
```
IWorkbook wb;
ISheet ws;
using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table.xls", FileMode.Open, FileAccess.Read))
{
wb = new HSSFWorkbook(file);
}
ws = wb.GetSheetAt(0);
ws.SetDefaultColumnStyle(4, wb.CreateCellStyle());

using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table_result.xls", FileMode.Create, FileAccess.Write))
{
wb.Write(file);
}
```
For the column E the call of SetDefaultColumnStyle works as expected. But the Colors of the cells in the other columns change. When I compare the original file with the file created by NPOI some of the cell colors are changed.
The description of SetDefaultColumnStyle says "Sets the default column style for a given column. POI will only apply this style to new cells Added to the sheet.". So this function should have no influence on any cell which already has its own cell style, didn't it? From my Point of view is a bug.
To proof that the color change is not a General Problem I also opened the workbook and saved it without changes. The resulting file is smaller than the original Excel file but the content and the formatting keeps the same. So, the Color change of the cells must have something to do with the call to SetDefaultColumnStyle.
It would be great if someone could tell me if there is another possibility to remove the default column style from a column, which does not affect the existing cell styles of the sheet. Furthermore, I hope that this behavior can be fixed in the next stable release of NPOI.
My testing workbook is attatched.

Best regards
Herbie
Comments: ** Comment from web user: Herbiee **

Thank you for the fast reply.
I've got a similar issue with XSSF of which I'm not sure if it is related to the problem above. Since both problems have to do with color changes, they could be related to each other.

I've saved the sample table I've attached above as .xlsx file and did the same tests for XSSF. First I thought that XSSF has the same problem, but the color change was a different. Then I saw that when I remove the call to SetDefaultColumnStyle (saving the .xlsx file without changes) still changes the color in column C. So, for XSSF the function SetDefaultColumnStyle seems to work, but therefore some colors seem to change generally when re-saving the document.

I hope that both behaviors have the same root cause.

Commented Unassigned: Incorrect value of calculation in XSSFWorkbook [13108]

$
0
0
In the next example I try to calculate formula "B1/A1":
```
var workbook = new XSSFWorkbook();

var sheet = workbook.CreateSheet();

var row = sheet.CreateRow(0);

var cell = row.CreateCell(0);
cell.SetCellType(CellType.Numeric);
cell.SetCellValue(150.0);

cell = row.CreateCell(1);
cell.SetCellType(CellType.Numeric);
cell.SetCellValue(25.0);

cell = row.CreateCell(2);
cell.SetCellType(CellType.Formula);
cell.SetCellFormula("B1/A1");

workbook.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll();

```
Result in the C1 cell should be 0.16. However, the C1 cell returns 166666666666667.0. When I replace workbook creation to the:
```
var workbook = new HSSFWorkbook();
```
it works fine. But this implementation of IWorkbook has limitation in row count (max 65K). How can I fix this bug in XSSFWorkbook?
Comments: ** Comment from web user: Herbiee **

This issue seems to be fixed in NPOI 2.1.1. If I run the code above I get the "NumericCellValue" 0.166666666666667 for XSSFWorkbook.

Commented Unassigned: Incorrect value of calculation in XSSFWorkbook [13108]

$
0
0
In the next example I try to calculate formula "B1/A1":
```
var workbook = new XSSFWorkbook();

var sheet = workbook.CreateSheet();

var row = sheet.CreateRow(0);

var cell = row.CreateCell(0);
cell.SetCellType(CellType.Numeric);
cell.SetCellValue(150.0);

cell = row.CreateCell(1);
cell.SetCellType(CellType.Numeric);
cell.SetCellValue(25.0);

cell = row.CreateCell(2);
cell.SetCellType(CellType.Formula);
cell.SetCellFormula("B1/A1");

workbook.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll();

```
Result in the C1 cell should be 0.16. However, the C1 cell returns 166666666666667.0. When I replace workbook creation to the:
```
var workbook = new HSSFWorkbook();
```
it works fine. But this implementation of IWorkbook has limitation in row count (max 65K). How can I fix this bug in XSSFWorkbook?
Comments: ** Comment from web user: mukminoff **

NPOI 2.1.1 is beta, unfortunally.

Created Unassigned: SetSheetOrder breaks XLSX [13142]

$
0
0
NPOI 2.1
XSSFWorkbook.cs

Issue:
When setting the sheet order (i.e. moving a worksheet) with XSSF, the XLSX file will break and cannot be opened with Excel.

Reason:
CT_Sheet.Set will set the SheetId of the moved sheet to 0, which the OpenXML format does not allow.

our workaround:
keep the sheetId
```
public void SetSheetOrder(String sheetname, int pos)
{
int idx = GetSheetIndex(sheetname);
XSSFSheet sheet = sheets[idx];
sheets.RemoveAt(idx);
sheets.Insert(pos,sheet);
// Reorder CT_Sheets
CT_Sheets ct = workbook.sheets;
CT_Sheet cts = ct.GetSheetArray(idx).Copy();

// get sheetid from sheet to be moved
uint Mysheetid = ct.GetSheetArray(idx).sheetId;

workbook.sheets.RemoveSheet(idx);
CT_Sheet newcts = ct.InsertNewSheet(pos);
newcts.Set(cts);

// retrieve sheetid
newcts.sheetId = Mysheetid;

//notify sheets
for (int i = 0; i < sheets.Count; i++)
{
sheets[i].sheet = ct.GetSheetArray(i);
}
}
```

Updated Release: NPOI 2.1.2

Commented Issue: xlsx文件删除sheet后存储文件,然后使用鼠标打开文件时报错 [13136]

$
0
0
运行代码:
class Program
{
static void Main(string[] args)
{
FileStream file = new FileStream("test.xlsx", FileMode.Open);//读入excel模板

IWorkbook workbook = new XSSFWorkbook(file);
workbook.RemoveSheetAt(0);

file = new FileStream("test1.xlsx", FileMode.Create);//写入excel
workbook.Write(file);

file.Close();
}
}
执行完成后,使用鼠标打开test1.xlsx文件,报丢失信息错误
Comments: ** Comment from web user: tonyqus **

确认是个bug,将在下个版本修复

Commented Issue: xlsx文件删除sheet后存储文件,然后使用鼠标打开文件时报错 [13136]

$
0
0
运行代码:
class Program
{
static void Main(string[] args)
{
FileStream file = new FileStream("test.xlsx", FileMode.Open);//读入excel模板

IWorkbook workbook = new XSSFWorkbook(file);
workbook.RemoveSheetAt(0);

file = new FileStream("test1.xlsx", FileMode.Create);//写入excel
workbook.Write(file);

file.Close();
}
}
执行完成后,使用鼠标打开test1.xlsx文件,报丢失信息错误
Comments: ** Comment from web user: skyv158 **

老大,下个版本预计什么时间发布呀?


Commented Unassigned: SetSheetOrder breaks XLSX [13142]

$
0
0
NPOI 2.1
XSSFWorkbook.cs

Issue:
When setting the sheet order (i.e. moving a worksheet) with XSSF, the XLSX file will break and cannot be opened with Excel.

Reason:
CT_Sheet.Set will set the SheetId of the moved sheet to 0, which the OpenXML format does not allow.

our workaround:
keep the sheetId
```
public void SetSheetOrder(String sheetname, int pos)
{
int idx = GetSheetIndex(sheetname);
XSSFSheet sheet = sheets[idx];
sheets.RemoveAt(idx);
sheets.Insert(pos,sheet);
// Reorder CT_Sheets
CT_Sheets ct = workbook.sheets;
CT_Sheet cts = ct.GetSheetArray(idx).Copy();

// get sheetid from sheet to be moved
uint Mysheetid = ct.GetSheetArray(idx).sheetId;

workbook.sheets.RemoveSheet(idx);
CT_Sheet newcts = ct.InsertNewSheet(pos);
newcts.Set(cts);

// retrieve sheetid
newcts.sheetId = Mysheetid;

//notify sheets
for (int i = 0; i < sheets.Count; i++)
{
sheets[i].sheet = ct.GetSheetArray(i);
}
}
```
Comments: ** Comment from web user: tonyqus **

The bug looks to be caused by CT_Sheet.Copy

New Post: add pdf stream into worksheet

$
0
0
I met the same situation too,Dose NPOI have this function?I just saw addPicture,But I need to Add Object.

the tranditional way is :
            public void AddObj(string objPath, float incrementLeft, float incrementTop, float height)
    {
        try
        {
            OLEObjects objs = (OLEObjects)workSheet.OLEObjects(Type.Missing);

            OLEObject obj = objs.Add(Type.Missing, objPath, false, false, false, Type.Missing, Type.Missing, incrementLeft, incrementTop, 21, height);

            obj.ShapeRange.Height = height;

        }
        catch (Exception e)
        {
            this.KillExcelProcess();
            throw e;
        }
    }

How should I implement it by NPOI?

New Post: Adding more than one chart to worksheet results in corrupt .XLSX file

$
0
0
I am trying to create an Excel worksheet containing multiple charts. However, when I add a second chart the resulting .XLSX file is corrupt when I try opening it.

Here is a code sample that shows the issue (modified from the LineChart example):
 static void Main(string[] args)
        {
            int NUM_OF_CHARTS = 2;

            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("linechart");

            IDrawing drawing = sheet.CreateDrawingPatriarch();

            for (int i = 0; i < NUM_OF_CHARTS; i++)
            {
                CreateChart(sheet, drawing, i);
            }

            using (FileStream fs = File.Create(String.Format("test-{0} charts-{1}.xlsx", NUM_OF_CHARTS, DateTime.Now.Ticks)))
            {
                wb.Write(fs);
            }
        }

        static IChart CreateChart(ISheet sheet, IDrawing drawing, int factor)
        {
            int NUM_OF_ROWS = 3;
            int NUM_OF_COLUMNS = 10;

            Random r = new Random((int)DateTime.Now.Ticks);

            System.Threading.Thread.Sleep(r.Next(50, 100));

            // Create a row and put some cells in it. Rows are 0 based.
            IRow row;
            ICell cell;
            for (int rowIndex = 0 + (NUM_OF_ROWS * factor); rowIndex < NUM_OF_ROWS * (1 + factor); rowIndex++)
            {
                row = sheet.CreateRow((short)rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
                {
                    cell = row.CreateCell((short)colIndex);
                    if (rowIndex == 0 + NUM_OF_ROWS * factor)
                    {
                        cell.SetCellValue(colIndex);
                    }
                    else
                    {
                        cell.SetCellValue(r.Next(0,100));
                    }
                }
            }

            IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, 10, 0 + (10 * factor), 20, 10 + (10 * factor));

            IChart chart = drawing.CreateChart(anchor);
            ILineChartData<double, double> data = chart.GetChartDataFactory().CreateLineChartData<double, double>();

            // Use a category axis for the bottom axis.
            IChartAxis bottomAxis = chart.GetChartAxisFactory().CreateCategoryAxis(AxisPosition.Bottom);
            IValueAxis leftAxis = chart.GetChartAxisFactory().CreateValueAxis(AxisPosition.Left);

            IChartDataSource<double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0 + factor * NUM_OF_ROWS, 0 + factor * NUM_OF_ROWS, 0, NUM_OF_COLUMNS - 1));
            IChartDataSource<double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1 + factor * NUM_OF_ROWS, 1 + factor * NUM_OF_ROWS, 0, NUM_OF_COLUMNS - 1));
            IChartDataSource<double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2 + factor * NUM_OF_ROWS, 2 + factor * NUM_OF_ROWS, 0, NUM_OF_COLUMNS - 1));

            var s1 = data.AddSerie(xs, ys1);
            s1.SetTitle("title1");
            var s2 = data.AddSerie(xs, ys2);
            s2.SetTitle("title2");

            chart.Plot(data, bottomAxis, leftAxis);

            return chart;
        }
Any ideas?

Created Unassigned: EXCEL 2013 - Date Format from row 10 [13144]

$
0
0
Hi, I'm using the NPOI 2.0 Version of your library, and I'm having an issue when creating an Excel file and try to open with Microsoft Excel 2013.

There is a column with date format, that works fine from row 1 to 10, but from row 11 the program stops reading this property and shows a weird decima value.

The llibrary works perfect for me in older versions of Excel, OpenOffice and GoogleDrive.

I attach a screenshot.

Thanks and regards!

Alejo

New Post: add pdf stream into worksheet

New Post: HSSF How to remove/delete Column Style?

$
0
0
Hello.
I have two questions, but first a short description of what I'm trying to do.
I try to change existing Worksheets with my application. One of my functions deletes columns. To remove a column I remove all cells in this column row by row and shift the Column Styles from my column index+1 to the last column index by 1 to the left.

To shift a Column Style I first tried the following code.
ICellStyle columnStyle = sheet.GetColumnStyle(columnIndex + 1);
sheet.SetDefaultColumnStyle(columnIndex, columnStyle);
This only worked if the column at columnIndex+1 had a column style set. Otherwise GetColumnStyle returns null and SetDefaultColumnStyle seems not to accept this value, since I got a NullReferenceException.
My solution is the following code.
ICellStyle columnStyle = sheet.GetColumnStyle(columnIndex + 1);
if (columnStyle != null)
    sheet.SetDefaultColumnStyle(columnIndex, columnStyle);
else
    sheet.SetDefaultColumnStyle(columnIndex, sheet.Workbook.CreateCellStyle());
The result generally looks like I've expected, but when I now call
columnStyle = sheet.GetColumnStyle(columnIndex);
columnStyle is not null even if the right-hand columns style is null.

So, my questions are. Is it possible to set remove/delete the style of a column and row? Is it possible to get the index of the last column with a column style? (since this must not be the same as the last column with content)

I hope you can help me with this problem.

Best regards,
Herbie

Commented Unassigned: EXCEL 2013 - Date Format from row 10 [13144]

$
0
0
Hi, I'm using the NPOI 2.0 Version of your library, and I'm having an issue when creating an Excel file and try to open with Microsoft Excel 2013.

There is a column with date format, that works fine from row 1 to 10, but from row 11 the program stops reading this property and shows a weird decima value.

The llibrary works perfect for me in older versions of Excel, OpenOffice and GoogleDrive.

I attach a screenshot.

Thanks and regards!

Alejo
Comments: ** Comment from web user: tonyqus **

looks weird. I suggest you double check your code. Cell styles are not applied correctly.


Commented Unassigned: NPOI 2.1.1 HSSF SetDefaultColumnStyle changes cell color styles in whole sheet [13138]

$
0
0
Hello.
I'm new to NPOI and if I misunderstand something please correct me. I want to write a test application which opens an existing Excel Workbook (created by Excel 2007 or 2010 in 97-2003 xls Format) and removes the column style of column index 4. The first sheet in the Workbook includes several columns formatted in different value formats like Date and Text and different cell colors.
The only function I've found to set a column style was SetDefaultColumnStyle of ISheet.
Here is the code:
```
IWorkbook wb;
ISheet ws;
using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table.xls", FileMode.Open, FileAccess.Read))
{
wb = new HSSFWorkbook(file);
}
ws = wb.GetSheetAt(0);
ws.SetDefaultColumnStyle(4, wb.CreateCellStyle());

using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table_result.xls", FileMode.Create, FileAccess.Write))
{
wb.Write(file);
}
```
For the column E the call of SetDefaultColumnStyle works as expected. But the Colors of the cells in the other columns change. When I compare the original file with the file created by NPOI some of the cell colors are changed.
The description of SetDefaultColumnStyle says "Sets the default column style for a given column. POI will only apply this style to new cells Added to the sheet.". So this function should have no influence on any cell which already has its own cell style, didn't it? From my Point of view is a bug.
To proof that the color change is not a General Problem I also opened the workbook and saved it without changes. The resulting file is smaller than the original Excel file but the content and the formatting keeps the same. So, the Color change of the cells must have something to do with the call to SetDefaultColumnStyle.
It would be great if someone could tell me if there is another possibility to remove the default column style from a column, which does not affect the existing cell styles of the sheet. Furthermore, I hope that this behavior can be fixed in the next stable release of NPOI.
My testing workbook is attatched.

Best regards
Herbie
Comments: ** Comment from web user: Herbiee **

Hi, it's me again.
I tried to debug the issue a little bit by myself. Until now I can say that the colors are not to change by the call to _wb.SetDefaultColumnStyle_.
The call "_wb.CreateCellStyle();_" causes the color change.
If I change the code above to
```
IWorkbook wb;
ISheet ws;
using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table.xls", FileMode.Open, FileAccess.Read))
{
wb = new HSSFWorkbook(file);
}
ws = wb.GetSheetAt(0);
ws.SetDefaultColumnStyle(4, wb.GetCellStyleAt(15));

using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table_result.xls", FileMode.Create, FileAccess.Write))
{
wb.Write(file);
}
```
the colors do not change. When I only open the Workbook create a new cell style without using it and safe the workbook, the colors are changed afterwards.

I hope that helps to find the root cause.

Best regards
Herbie

Updated Wiki: Home

$
0
0
This project is the .NET version of POI Java project at http://poi.apache.org/. POI is an open source project which can help you read/write xls, doc, ppt files. It has a wide application.
For example, you can use it to
a. generate a Excel report without Microsoft Office suite installed on your server and more efficient than call Microsoft Excel ActiveX at background;
b. extract text from Office documents to help you implement full-text indexing feature (most of time this feature is used to create search engines).
c. extract images from Office documents
d. generate Excel sheets that contains formulas

To get the latest code, please visit https://github.com/tonyqus/npoi.
 
 
Donate NPOI
donate_btn
 
Bitcoin payment address: 19GY7rqsXo7nMWs3XuMXh9robV6HrUFDGX
 
NPOI Support Service
We provide onsite/offsite hand-on training, troubleshooting and software customization. For details, please contact support@neuzilla.com
欢迎去恩育软件定制商店选购商品,有定制需求请联系Tony的QQ: 43518424
 
Advantage of NPOI
a. It's totally free to use
b. Cover most features of Excel (cell style, data format, formula and so on)
c. Support xls, xlsx, docx.
d. Designed to be interface-oriented (take a look at NPOI.SS namespace)
e. Support not only export but also import
f. .Net 2.0 based even for xlsx and docx (though we also support .NET 4.0)
g. Successful cases from all over the world
h. huge amout of basic examples
i. No dependency on isolated storage
 
Comments from NPOI users
image
image
image
image
image
 
Tutorial

NPOI on SNS
中文
QQ群: 189925337
 
English

System Requirement
VS2010 with .NET 4.0 runtime
VS2005 or VS2008 with .NET 2.0 Runtime (SP1)
vs2003 with .NET 1.1
Mono
medium trust environment in ASP.NET
 
Extensions
 
 

New Post: NPOI 2.1.1 is useless with XLSX file

$
0
0
Hello,

I am using latest NPOI to open and to save an XLSX file.

That XLSX file has a lot of sheets with tables and graphics. The test I made was only to open and immediately to save the file.

After that, I tried to open the Excel file and the file appeared corrupt. Excel has told me to fix it and I pressed OK. After I pressed OK, the file was fixed and all the sheets are present, however, graphics appeared bad formated.

Any help on this please?

Thanks in advance,
Jaime

New Post: NPOI 2.1.1 is useless with XLSX file

$
0
0
looks to be a bug. Please submit your xlsx file and the repro steps to Issues.

New Comment on "《我与Selerant》系列文章"

$
0
0
唉,为了利益,出卖自己灵魂的人太多了。 楼主是高人,早走早好。
Viewing all 1621 articles
Browse latest View live


Latest Images

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