1

EDIT : For the need of my project I'll work with EPPlus (Thanks to the Tsahi Asher's comment) package which fit perfectly with what we need to manipulate Excel and wrap OpenXML.
My problem for this project is so solved, but I'm still pretty curious about the answer of my question, so feel free to answer, would be really appreciated.

I'm working on a project where I have to generate an Excel file and then email it.
In order we have problems make Interop work for now, we're using OpenXML to create our Excel files, so it's pretty new to our team (1 month or something like that).

I don't have that many problems with creating the file, which is a simple document with 3 tables of two columns.
The problem is that when I try to open the .xslx file previously generated, I got a pop up which tell me:

We found a problem with some content in 'file.xlsx'. Do you want us to try to recover as much as we can ? If you trust the source of this workbook, click Yes.

If I click "Yes", it repairs the file and it shows up exactly as I'd like to, with a message:

Repaired Records: Table from /xl/tables/table2.xml part (Table)

Here's the short code where I create this table :

SpreadsheetDocument xl = SpreadsheetDocument.Create(pathID, SpreadsheetDocumentType.Workbook);
WorkbookPart wbp = xl.AddWorkbookPart();
WorksheetPart wsp = wbp.AddNewPart<WorksheetPart>();
Workbook wb = new Workbook();
DocumentFormat.OpenXml.Spreadsheet.FileVersion fv = new DocumentFormat.OpenXml.Spreadsheet.FileVersion();
fv.ApplicationName = "Microsoft Office Excel";
Worksheet ws = new Worksheet();
SheetData sd = new SheetData();

WorkbookStylesPart wbsp = wbp.AddNewPart<WorkbookStylesPart>();
wbsp.Stylesheet = CreateStylesheet();
wbsp.Stylesheet.Save();

Columns columns = new Columns();
columns.Append(CreateColumnData(1, 1, 110));
columns.Append(CreateColumnData(2, 2, 20));
ws.Append(columns);

// Insert data in cells

ws.Append(sd);
wsp.Worksheet = ws;
wsp.Worksheet.Save();
Sheets sheets = new Sheets();
Sheet sheet = new Sheet();

CultureInfo ciCurr = CultureInfo.CurrentCulture;
sheet.Name = "Week " + ciCurr.Calendar.GetWeekOfYear(now.AddDays(-7),
  CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
sheet.SheetId = 1;
sheet.Id = wbp.GetIdOfPart(wsp);
sheets.Append(sheet);
wb.Append(fv);
wb.Append(sheets);

TableParts tableParts = new TableParts() { Count = (UInt32Value)3U };
tableParts.Append(new TablePart() { Id = "rId1" });
tableParts.Append(new TablePart() { Id = "rId2" });
tableParts.Append(new TablePart() { Id = "rId3" });

wsp.Worksheet.Append(tableParts);

// First Table creation, exactly same as 2nd except column names

// Beginning of second table creation
TableDefinitionPart tableDefinitionPart2 = wsp.AddNewPart<TableDefinitionPart>("rId2");
var tableDimension2 = "A" + _savedIndex1 + ":B" + (_savedIndex1 + 4);
Table table2 = new Table()
    {
        Id = (UInt32Value)2U,
        Name = "Table2",
        DisplayName = "Table2",
        Reference = tableDimension2,
        TotalsRowShown = false,
    };

TableColumns tableColumns2 = new TableColumns() { Count = (UInt32Value)2U };

TableColumn tableColumn12 = new TableColumn() { Id = (UInt32Value)1U, Name = "Product Name" };
TableColumn tableColumn22 = new TableColumn() { Id = (UInt32Value)2U, Name = "Total qty" };

tableColumns2.Append(tableColumn12);
tableColumns2.Append(tableColumn22);

TableStyleInfo tableStyleInfo2 = new TableStyleInfo()
    {
        Name = "TableStyleLight1",
        ShowFirstColumn = false,
        ShowLastColumn = false,
        ShowRowStripes = true,
        ShowColumnStripes = false,
    };

table2.Append(tableColumns2);
table2.Append(tableStyleInfo2);
tableDefinitionPart2.Table = table2;
// End of second table creation

// Third Table creation, exactly same as 2nd except column names

xl.WorkbookPart.Workbook = wb;
xl.WorkbookPart.Workbook.Save();
xl.Close();

I already tried to directly search in xml files by unzipping the .xlxs but I didn't noticed any differences between this Table2 and Table1/Table3.
Even after Excel repaired the file, I tried to unzip again and what Excel added to Table2 was also added to Table1/Table3, so I really have no guess why this Table2 is stopping this file to open properly.
I also tried to remove Table2 and left Table1/Table3 as they are (only change id) and it worked perfectly, so I'm pretty sure that my problem really comes from Table2.

So, where does my problem come from? What's my mistakes and what have I missed?

Any hint would be appreciated as OpenXml is pretty new to me.

Zoma
  • 321
  • 7
  • 20
  • 2
    I strongly recommend using [EPPlus](https://www.nuget.org/packages/EPPlus/) for that. It's a wrapper around the OpenXML API, which greatly simplifies generating Excel files. The Excel file format is pretty complex, and you have to know the exact XML schema to use the OpenXML SDK, while EPPlus does that for you. – Tsahi Asher Feb 27 '19 at 09:10
  • @TsahiAsher Thanks for the suggestion, I'll take a look at this and propose it to our team if it fits our needs – Zoma Feb 27 '19 at 09:16
  • @TsahiAsher Thanks a lot for this package, we'll use it for work with Excel for now. Added an EDIT to inform that I got a solution even if I'm still curious about what's the problem here. – Zoma Feb 27 '19 at 15:00
  • Are you able to show what you are populating cell A<_savedIndex1> and B<_savedIndex1> with? I have a hunch that's the issue. – petelids Feb 27 '19 at 16:27
  • @petelids I just fill it with a string for A column (format -> "reference1/reference2 name") and a double for B (but there's never decimals, so basically same as int in cells). Both columns are left with default numberFormat because it doesn't really matter here. For the third column quantity column (B) is left empty but there's still the product name. – Zoma Feb 28 '19 at 07:23
  • 1
    I _think_ the contents of the header cells of the table need to match the `name` of the `TableColumn`. – petelids Feb 28 '19 at 17:56
  • @petelids I don't think the problem comes from here as they match :/ – Zoma Mar 01 '19 at 07:21

1 Answers1

0

When adding a table, make sure that the names of the column headers are not repeated. If you have duplicates column names Excel throw this error and will fix it by renaming the 2nd duplicated column by adding a number a the end. See example below:

| QTY | UNIT # | QTY2 |

See that 2nd QTY was renamed to QTY2 by excel recovery process.

Gi1ber7
  • 632
  • 1
  • 11
  • 22
  • 1
    Thank you for your interest in this question. It's been some time so I don't remember the exact specifications and don't anymore have access to the code, but based on the code I provided when writing the question I didn't have any column that had the same name as another one – Zoma Aug 11 '22 at 17:33
  • 1
    Hi, I just wanted to point out another possible cause. I might help someone else. – Gi1ber7 Aug 11 '22 at 17:41