简体   繁体   English

OOxml 或 Office OpenXML C# - 从 DataTable 导出到 excel boolean 数据类型导致修复 ZBF57C906FA7D2BB65DEZ07

[英]OOxml or Office OpenXML C# - export from DataTable to excel boolean data type causes repair excel

I am exporting C# DataTable data to excel, I am getting excel repair error for boolean datatype column.我正在将 C# DataTable 数据导出到 excel,我收到 excel 修复错误 Z84E2C64F38F78BA3EA5C29 following is code I am using for boolean datatype.以下是我用于 boolean 数据类型的代码。

var exception = true;
var cellDataRow = new Cell();                    
cellDataRow.CellValue = new CellValue(BooleanValue.FromBoolean(exception));                                   cellDataRow.DataType = CellValues.Boolean;

Use 0 or 1 to represent false or true respectively.分别用0 或 1表示假或真

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Data;
using System.IO;

namespace ConsoleApp7
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
               

                WorkbookPart workbookPart = null;

                try
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var excel = SpreadsheetDocument.Create(memoryStream, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook, true))
                        {
                            workbookPart = excel.AddWorkbookPart();
                            workbookPart.Workbook = new Workbook();
                            uint sheetId = 1;
                            excel.WorkbookPart.Workbook.Sheets = new Sheets();
                            Sheets sheets = excel.WorkbookPart.Workbook.GetFirstChild<Sheets>();

                            DataSet dataSet = new DataSet();

                            for (int i = 0; i < 5; i++)
                            {
                                DataTable dataTable = new DataTable();
                                dataTable.TableName = "Table" + (i + 1).ToString();

                                dataTable.Columns.Add("Column1", typeof(string));
                                dataTable.Columns.Add("Column2", typeof(string));
                                dataTable.Columns.Add("Column3", typeof(string));
                                dataTable.Columns.Add("Column4", typeof(string));
                                dataTable.Columns.Add("Column5", typeof(string));


                                for (int j = 0; j < 5; j++)
                                {
                                    DataRow dataRow = dataTable.NewRow();

                                    for (int k = 0; k < dataTable.Columns.Count; k++)
                                    {
                                        dataRow[k] = 0; // or 1
                                    }

                                    dataTable.Rows.Add(dataRow);
                                }

                                dataSet.Tables.Add(dataTable);

                            }


                            for (int i = 0; i < dataSet.Tables.Count; i++)
                            {
                                string relationshipId = "rId" + (i + 1).ToString();
                                WorksheetPart wSheetPart = workbookPart.AddNewPart<WorksheetPart>(relationshipId);
                                string sheetName = dataSet.Tables[i].TableName;
                                Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
                                sheets.Append(sheet);

                                Worksheet worksheet = new Worksheet();

                                wSheetPart.Worksheet = worksheet;

                                SheetData sheetData = new SheetData();
                                worksheet.Append(sheetData);

                                string[] excelColumns = new string[] { "A", "B", "C", "D", "E", "F", "G" };

                                for (int l = 0; l < dataSet.Tables[i].Rows.Count; l++)
                                {
                                    for (int m = 0; m < dataSet.Tables[i].Columns.Count; m++)
                                    {
                                        AddToCell(sheetData, Convert.ToUInt32(l + 1), excelColumns[m], CellValues.Boolean, Convert.ToString(dataSet.Tables[i].Rows[l][m]));
                                    }
                                }


                                sheetId++;
                            }


                            excel.Close();
                        }

                        FileStream fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "MultipleWorkSheet.xlsx", FileMode.Create, FileAccess.Write);
                        memoryStream.WriteTo(fileStream);
                        fileStream.Close();
                        memoryStream.Close();
                    }
                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }
            catch (Exception ex)
            {

                // logging, etc.
            }
        }

        private static void AddToCell(SheetData sheetData, UInt32 uint32rowIndex, string strColumnName, DocumentFormat.OpenXml.EnumValue<CellValues> CellDataType, string strCellValue)
        {
            Row row = new Row() { RowIndex = uint32rowIndex };
            Cell cell = new Cell();

            cell = new Cell();
            cell.CellReference = strColumnName + row.RowIndex.ToString();
            cell.DataType = CellDataType;
            cell.CellValue = new CellValue(strCellValue);
            row.AppendChild(cell);

            sheetData.Append(row);
        }
    }
}

如何从列表中的 C# 导出数据<object>或 IEnumerable 到 Excel (MS OpenXml)?<div id="text_translate"><p> 我正在尝试使用 Microsoft 的 Open XML 库将List&lt;dynamic&gt;或IEnumerable导出到 Excel 文件。</p><p> 库: <a href="https://github.com/OfficeDev/Open-XML-SDK" rel="nofollow noreferrer">https://github.com/OfficeDev/Open-XML-SDK</a></p><p> 类似请求的示例: <a href="https://www.codeproject.com/Articles/692121/Csharp-Export-data-to-Excel-using-OpenXML-librarie" rel="nofollow noreferrer">https://www.codeproject.com/Articles/692121/Csharp-Export-data-to-Excel-using-OpenXML-librarie</a></p><p> 上面的例子工作得很好,但之间有很多代码(到目前为止工作很整洁,但我正在寻找优化)</p><p> 但是这个库的文档没有扩展(或者没有很多例子),我看到的例子是关于如何将数据导出到 Excel 它使用DataTable或Dataset ; 因此,他们进行从List到DataTable的对话以导出DataTable 。 对于其他库更容易解决的问题,这似乎相当复杂。</p><p> 因此,如果有人有一个关于如何快速和通用导出的示例,我们将不胜感激。</p></div></object> - How to export data from C# from List<object> or IEnumerable to Excel (MS OpenXml)?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中使用文本颜色将数据从excel导出到数据表 - export data from excel to datatable with text color in C# 如何从列表中的 C# 导出数据<object>或 IEnumerable 到 Excel (MS OpenXml)?<div id="text_translate"><p> 我正在尝试使用 Microsoft 的 Open XML 库将List&lt;dynamic&gt;或IEnumerable导出到 Excel 文件。</p><p> 库: <a href="https://github.com/OfficeDev/Open-XML-SDK" rel="nofollow noreferrer">https://github.com/OfficeDev/Open-XML-SDK</a></p><p> 类似请求的示例: <a href="https://www.codeproject.com/Articles/692121/Csharp-Export-data-to-Excel-using-OpenXML-librarie" rel="nofollow noreferrer">https://www.codeproject.com/Articles/692121/Csharp-Export-data-to-Excel-using-OpenXML-librarie</a></p><p> 上面的例子工作得很好,但之间有很多代码(到目前为止工作很整洁,但我正在寻找优化)</p><p> 但是这个库的文档没有扩展(或者没有很多例子),我看到的例子是关于如何将数据导出到 Excel 它使用DataTable或Dataset ; 因此,他们进行从List到DataTable的对话以导出DataTable 。 对于其他库更容易解决的问题,这似乎相当复杂。</p><p> 因此,如果有人有一个关于如何快速和通用导出的示例,我们将不胜感激。</p></div></object> - How to export data from C# from List<object> or IEnumerable to Excel (MS OpenXml)? 使用 C# 从 SQL 数据集到 Excel 流的 OpenXml - OpenXml from SQL data set to Excel stream using C# 如何从c#中的数据集或数据表导出excel? - How to export excel from dataset or datatable in c#? 从C#网站的数据表中导出到Excel文件或.csv文件 - Export into excel file or .csv file from datatable of website in c# 使用 C# 从 ASP.NET 将 DataTable 导出到 Excel - Export DataTable to Excel with C# from ASP.NET 从特定列 C# 将数据表导出到 Excel Strat - Export Datatable into Excel Strat from Specific Column C# 无法在C#中使用epplus从dataTable中导出Excel文件 - Cannot export excel file from dataTable by using epplus in C# 如何在WPF C#中从数据表导出到Excel文件 - How to export from datatable to excel file in wpf c# 从C#导出数据并将数据格式化为Excel - Export and format data into Excel from C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM