Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数据从c控制台应用程序导出到Excel_C#_Excel - Fatal编程技术网

C# 如何将数据从c控制台应用程序导出到Excel

C# 如何将数据从c控制台应用程序导出到Excel,c#,excel,C#,Excel,我在c控制台输出中有一些行和列,我想将其导出到excel。有什么建议吗?谢谢我使用了Nuget的EPPlus库。下面是我的动态实现,它满足了我的需要。您可以在他们的文档中找到更简单的示例。但这将允许您根据Poco实际创建行/列 您可以使用以下方法: public void WriteFile<THeader, TEntity>(THeader header, params TEntity[] contents) { if (!File.Exists(F

我在c控制台输出中有一些行和列,我想将其导出到excel。有什么建议吗?谢谢

我使用了Nuget的EPPlus库。下面是我的动态实现,它满足了我的需要。您可以在他们的文档中找到更简单的示例。但这将允许您根据Poco实际创建行/列

您可以使用以下方法:

    public void WriteFile<THeader, TEntity>(THeader header, params TEntity[] contents)
    {
        if (!File.Exists(FileInformation.FullName))
            File.Create(FileInformation.FullName).Close();

        using (var excel = new ExcelPackage())
        {
            ExcelWorksheet worksheet = excel.Workbook.Worksheets.Add(FileInformation.Name);
            CreateExcelWorksheetHeader(worksheet, header);

            var row = 2;
            Dictionary<int, object> mapping = BuildTableMap(worksheet, 1);
            foreach (TEntity content in contents)
            {
                MapEntityToRow(worksheet, mapping, row, content);
                row++;
            }

            excel.SaveAs(FileInformation);
        }
    }

    private static void CreateExcelWorksheetHeader<THeader>(ExcelWorksheet worksheet, THeader entity)
    {
        var index = 1;
        PropertyInfo[] properties = typeof(THeader).GetProperties();
        foreach (PropertyInfo property in properties)
        {
            worksheet.Cells[1, index].Value = property.GetValue(entity, null);
            index++;
        }
    }

    private static void MapEntityToRow<TEntity>(ExcelWorksheet worksheet, Dictionary<int, object> table, int row, TEntity entity)
    {
        IDictionary<string, string> properties = ObjectMapper.GetPropertyNameAndAttribute<TEntity>();
        foreach (KeyValuePair<int, object> column in table)
        {
            var matchColumnToProperty = properties.SingleOrDefault(property => string.Compare(property.Key, (string)column.Value, true) == 0).Key;
            var matchColumnToAttribute = properties.SingleOrDefault(property => string.Compare(property.Value, (string)column.Value, true) == 0).Value;

            if (matchColumnToProperty != null)
                worksheet.Cells[row, column.Key].Value =
                    typeof(TEntity).GetProperty(matchColumnToProperty).GetValue(entity, null);

            if (matchColumnToAttribute != null)
                worksheet.Cells[row, column.Key].Value =
                    typeof(TEntity).GetProperty(properties.SingleOrDefault(property => string.Compare(property.Value, matchColumnToAttribute, true) == 0).Key)
                    .GetValue(entity, null);
        }
    }
我建议使用ClosedXML库。下面是一个简单的例子:

XLWorkbook workbook = new XLWorkbook();
DataTable dt = new DataTable() { TableName = "New Worksheet" };
DataSet ds = new DataSet();

//input data
var columns = new[] { "column1", "column2", "column3" };
var rows = new object[][] 
{
     new object[] {"1", 2, false },
     new object[] { "test", 10000, 19.9 }
};

//Add columns
dt.Columns.AddRange(columns.Select(c => new DataColumn(c)).ToArray());

//Add rows
foreach (var row in rows)
{
    dt.Rows.Add(row);
}

//Convert datatable to dataset and add it to the workbook as worksheet
ds.Tables.Add(dt);
workbook.Worksheets.Add(ds);

//save
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string savePath = Path.Combine(desktopPath, "test.xlsx");
workbook.SaveAs(savePath, false);

您有代码中的行吗?或者您只是想从控制台复制粘贴输出?有关您的数据的更多信息将有助于我们为您提供帮助-但您可以将数据输出到CSV,您可以在Excel中打开该CSV。如果您的数据位于DataGrid中,这里有一种快速的方法: