Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Excel was able to open the file by repairing or removing the unreadable content

$
0
0

Hi there,

I just started using Open XML SDK and created a program to generate XLSX. It works well but the only problem is that i get a warning message while opening the xlsx and then displays the data correctly.

"Excel was able to open the file by repairing or removing the unreadable content."
"Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded."

/xl/worksheets/sheet1.xml

/xl/worksheets/sheet2.xml

I tried to windiff the xml generated xml after correction and before correction by office but in vain. I guess the problem is related to coding and could anyone please shed some light on this issue?

Thanks,

Holy

I post the code below and here is steps I do on the code.

1 .Open an XLSX file (from template with no data)

2. Set an value on a particular cell

3. Fill data on sheet1

4. Fill data on sheet2

        public Stream Create(Stream fromXlsxTemplate, IDataReader data)
        {
            if (fromXlsxTemplate == null)
            {
                throw new ArgumentNullException("fromXlsxTemplate");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            fromXlsxTemplate.Position = 0;

            // Open SpreadSheet from Resource Stream (Open XML)
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fromXlsxTemplate, true))
            {
                WorkbookPart wbPart = document.WorkbookPart;
                Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().ElementAtOrDefault(0);

                WorksheetPart wsPart =
                    (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

                while (data.Read())
                {
                    WriteHeader(data, wbPart, wsPart);
                }

                data.NextResult();

                //Write to Sheet 1
                WriteSheet1(data, wsPart);

                //Move to result for sheet2
                data.NextResult();

                Sheet sheetDettaglo = wbPart.Workbook.Descendants<Sheet>().ElementAtOrDefault(1);

                WorksheetPart wsPartDet =
                    (WorksheetPart)(wbPart.GetPartById(sheetDettaglo.Id));

                //Write to Sheet 2
                WriteSheet2(data, wsPartDet);


            }

            return fromXlsxTemplate;
        }

        private void WriteHeader(IDataReader reader, WorkbookPart wbPart, WorksheetPart wsPart)
        {
            // A2 Cell
            Cell a2Cell = wsPart.Worksheet.Descendants<Cell>().
              Where(c => c.CellReference == "A2").FirstOrDefault();

            if (a2Cell != null)
            {
                string a2CellValue = a2Cell.InnerText;
                var stringTable = wbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
                if (stringTable != null)
                {
                    a2CellValue = stringTable.SharedStringTable.ElementAt(int.Parse(a2CellValue)).InnerText;
                }
                a2Cell.DataType = CellValues.String;
                a2Cell.CellValue = new CellValue(string.Format(a2CellValue, DateTime.Parse(reader.GetValue(0).ToString()).ToString("dd/MM/yyyy")));

            }
        }

        private uint WriteSheet1(IDataReader reader, WorksheetPart wsPart)
        {
            SheetData sheetData = wsPart.Worksheet.GetFirstChild<SheetData>();
            uint rowIdx = 6; // Start from row 6
            while (reader.Read())
            {
                Row newRow = new Row() { RowIndex = new DocumentFormat.OpenXml.UInt32Value(rowIdx) };

                //Value fo Cell Emissioni
                Cell cell = new Cell() { StyleIndex = (UInt32Value)1U };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("Emissioni")).ToString()); //
                newRow.AppendChild(cell);

                //Value fo Cell Operazioni
                cell = new Cell() { StyleIndex = (UInt32Value)9U };
                cell.DataType = CellValues.Number;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("Operazioni")).ToString());
                newRow.AppendChild(cell);

                //Value fo Cell Totale
                cell = new Cell() { StyleIndex = (UInt32Value)9U };
                cell.DataType = CellValues.Number;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("Totale")).ToString());
                newRow.AppendChild(cell);

                sheetData.Append(newRow);
                rowIdx++;
            }
            return rowIdx;
        }

        private uint WriteSheet2(IDataReader reader, WorksheetPart wsPartDet)
        {
            uint rowIdx;
            SheetData sheetDet = wsPartDet.Worksheet.GetFirstChild<SheetData>();
            rowIdx = 2; // Start from row 2

            while (reader.Read())
            {
                Row newRow = new Row() { RowIndex = new DocumentFormat.OpenXml.UInt32Value(rowIdx) };

                //n_polizza
                Cell cell = new Cell() { };
                cell.DataType = CellValues.Number;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("n_polizza")).ToString());
                newRow.AppendChild(cell);

                //c_attivita
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_attivita")).ToString());
                newRow.AppendChild(cell);

                //d_effetto
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("d_effetto")).ToString());
                newRow.AppendChild(cell);

                //descrizione
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("descrizione")).ToString());
                newRow.AppendChild(cell);

                //tipo_liquidazione
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("tipo_liquidazione")).ToString());
                newRow.AppendChild(cell);

                //d_carico
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("d_carico")).ToString());
                newRow.AppendChild(cell);

                //timestamp
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("timestamp")).ToString());
                newRow.AppendChild(cell);

                //d_assunzione
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("d_assunzione")).ToString());
                newRow.AppendChild(cell);

                //c_operazione_online
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_operazione_online")).ToString());
                newRow.AppendChild(cell);

                //c_prodotto
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_prodotto")).ToString());
                newRow.AppendChild(cell);

                //c_fondo 					
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_fondo")).ToString());
                newRow.AppendChild(cell);

                //descrizione_fondo
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("descrizione_fondo")).ToString());
                newRow.AppendChild(cell);

                //descrizione_prodotto
                cell = new Cell() { };
                cell.DataType = CellValues.Date;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("descrizione_prodotto")).ToString());
                newRow.AppendChild(cell);

                //importo
                cell = new Cell() { };
                cell.DataType = CellValues.Number;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("importo")).ToString());
                newRow.AppendChild(cell);

                //c_abi
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("c_abi")).ToString());
                newRow.AppendChild(cell);

                //descrizione_lv
                cell = new Cell() { };
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(reader.GetValue(reader.GetOrdinal("descrizione_lv")).ToString());
                newRow.AppendChild(cell);

                sheetDet.Append(newRow);
                rowIdx++;
            }
            return rowIdx;
        }


Viewing all articles
Browse latest Browse all 1288

Trending Articles



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