Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File with Pivot table corrupted after saving with NPOI #419

Closed
Khalidaba opened this issue Sep 23, 2020 · 14 comments
Closed

File with Pivot table corrupted after saving with NPOI #419

Khalidaba opened this issue Sep 23, 2020 · 14 comments
Labels
bug file_error file format generation/writing issue
Milestone

Comments

@Khalidaba
Copy link

Hello,
cannot open the file after manipulate it on my projet even if i dont do anything on it like this :

` //i get my file from the directory where it save
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
workbook = new XSSFWorkbook(fs);

            //And save it in an other folder
             try
            {
                workbook.Write(bos);
            }
            finally
            {
                bos.Close();
            }
            fileContents = bos.ToByteArray();
            File.WriteAllBytes(@"C:\user\New folder\test.xlsx", fileContents);

`
when i open it say
OpenFailMessage
and here the error screen when i trust the source
errounreadable

it seems the color and theme dont load correctly but cells and sheets (even those hide) are here ...
BEFOR
Original
After
After

testPivot.xlsx

@tonyqus tonyqus added this to the NPOI vNext milestone Sep 23, 2020
@tonyqus tonyqus added file_error file format generation/writing issue bug and removed enhancement labels Oct 8, 2021
@tonyqus tonyqus modified the milestones: NPOI vNext, NPOI 2.5.6 Oct 8, 2021
@tonyqus tonyqus changed the title Pivot xlsx file file with Pivot table corrupted after saving with NPOI Oct 8, 2021
@tonyqus tonyqus changed the title file with Pivot table corrupted after saving with NPOI File with Pivot table corrupted after saving with NPOI Oct 8, 2021
@tonyqus
Copy link
Member

tonyqus commented Oct 8, 2021

There is a big gap in pivotTable openxml serialization. I'm scheduling a fix in NPOI 2.5.6

@Syrlander
Copy link

@tonyqus I would like to take a look at the issue, as I'm working with a spreadsheet containing multiple pivot tables. Currently the only package I can get to work in my case is the Microsoft Interop package for Excel which is incredibly slow.

I don't have much prior experience working with OpenXML, could you maybe point me in the direction of some related documentation?

@tonyqus
Copy link
Member

tonyqus commented Nov 23, 2021

@Syrlander thank you for your interest. Maybe OpenXML SDK is a good choice for you. But I definitely welcome you contribute some code to NPOI. I do need a hand on this project.

There are some open source documents for OpenXML for you to get started with
https://docs.microsoft.com/en-us/office/open-xml/working-with-pivottables
dotnet/Open-XML-SDK#612
https://www.todaysoftmag.com/article/848/getting-started-with-openxml

Please keep these questions in mind.

  1. Where are these pivot table defined? In which openxml files?
  2. the goal of these openxml files such as pivottable.xml, pivotcachedefinition and so on
  3. how are these pivot elements embeded into sheet.xml (sheet.xml is the main definition of each sheet)

Btw, do you use telegram? Maybe you can join the NPOI user group at https://t.me/npoidevs

@tonyqus
Copy link
Member

tonyqus commented Nov 23, 2021

#622 #697

@tonyqus
Copy link
Member

tonyqus commented Nov 23, 2021

Pivot table openxml code of NPOI is located at
https://github.com/nissl-lab/npoi/blob/master/OpenXmlFormats/Spreadsheet/PivotTable

@KnyazSh
Copy link

KnyazSh commented Nov 26, 2021

Hello, @tonyqus. I am actively working on solving this problem. I have already done a lot of work on adding the missing code. I anticipate that I will be submitting a PR for your review in the near future.

@solarding
Copy link

solarding commented Jan 24, 2022

Hello, @tonyqus. I am actively working on solving this problem. I have already done a lot of work on adding the missing code. I anticipate that I will be submitting a PR for your review in the near future.

Hi @KnyazSh, just tried your latest commits, unfortunately the error remains.
Are you still working on it?

@KnyazSh
Copy link

KnyazSh commented Jan 25, 2022

Hi @solarding, No. I thought I'd already finished. Can you write more about the errors and send me an example file?

@solarding
Copy link

Hi @solarding, No. I thought I'd already finished. Can you write more about the errors and send me an example file?

The file it created is corrupted that excel can not open it.

Portfolio2020.xlsx
Portfolio202201261723.xlsx

public void testc()
{
IWorkbook workbook;

        var fn = _PFAD + "Portfolio2020.xlsx";
        var newFn = _PFAD + $"Portfolio{DateTime.Now:yyyyMMddHHmm}.xlsx";
        File.Copy(fn, newFn);
        using (var fs = new FileStream(fn, FileMode.Open, FileAccess.Read))
        {
            workbook = WorkbookFactory.Create(fs);                
            var dict = GetStockInfos(workbook);

            var shtAnalysis = workbook.GetSheet("Analysis");
            for (int i = 1; i < 100; i++)
            {
                var r = shtAnalysis.GetRow(i);
                if (r == null) break;
                var stockname = r.GetCell(0)?.StringCellValue;
                if (string.IsNullOrWhiteSpace(stockname) || !dict.ContainsKey(stockname)) continue;
                var stockCount = r.GetCell(3)?.NumericCellValue;
                if (!stockCount.HasValue || stockCount.Value <= 0) continue;
                var price = 100;//Stock.GetQuote(dict[stockname]);
                var marketValue = price * stockCount.Value;
                var cellMarketValue = r.GetCell(5);
                cellMarketValue.SetCellValue(marketValue);
            }
            shtAnalysis.GetRow(0).GetCell(5).SetCellValue(DateTime.Today.ToShortDateString());
        }
        using (var fs1 = new FileStream(newFn, FileMode.OpenOrCreate, FileAccess.Write))
            workbook.Write(fs1);

        workbook.Close();

    }

@solarding
Copy link

Hi @KnyazSh could you reproduce the problem?

tonyqus added a commit that referenced this issue Feb 7, 2022
Add minimal support Pivot tables, Fix #419, Fix #262
@tonyqus
Copy link
Member

tonyqus commented Feb 7, 2022

I've created the new branch Pivot-table

@tonyqus tonyqus modified the milestones: NPOI 2.5.6, NPOI 2.5.7 Feb 13, 2022
@tonyqus
Copy link
Member

tonyqus commented Mar 19, 2022

@KnyazSh Hi, I'm thinking if I should involve your pivot table implementation in NPOI 2.5.6. Any thoughts?

@KnyazSh
Copy link

KnyazSh commented Mar 21, 2022

Hi @tonyqus and @solarding.
I'm sorry I've been gone so long. I made another correction (#786) on the file analysis from @solarding.
I also found a small error in the test example from @solarding: please don't use File.Copy(fn, newFn);, because it breaks the outgoing stream a little bit. Just remove it and everything will work for you. But, as I said above, a small problem I found and solved.

@JeanPhilippeLux
Copy link

Hi, do you have a date for the release of the new version with this fix please ? I try to compile NPOI 2.6.0 by myseleft but I get an error so I can't test the new version.

thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug file_error file format generation/writing issue
Projects
None yet
Development

No branches or pull requests

6 participants