简体   繁体   English

将 excel 文件保存为 csv 格式,无需在 .net 核心 3.1 中打开

[英]save excel file to csv format without opening in .net core 3.1

I have a situation where I need to download and save excel file(.xlsx) to.CSV format in .net core console application.我有一种情况,我需要下载 excel 文件(.xlsx)并将其保存为 .net 核心控制台应用程序中的 CSV 格式。

Since, Microsoft.Interop packages are not compatible with.Net core 3.1, what other approach I can use to save Excel file as.CSV?由于 Microsoft.Interop 包与.Net core 3.1 不兼容,我可以使用什么其他方法将 Excel 文件保存为.CSV?

Appreciate suggestions.欣赏建议。

This is a combination of multiple existing answers on SO.这是关于 SO 的多个现有答案的组合。

First is from here Convert the xlsx to a DataTable using ClosedXML首先是从这里使用 ClosedXML 将 xlsx 转换为 DataTable

using ClosedXML.Excel;
...
    public static DataTable GetDataFromExcel(string path, dynamic worksheet)
    {
        //Save the uploaded Excel file.


        DataTable dt = new DataTable();
        //Open the Excel file using ClosedXML.
        using (XLWorkbook workBook = new XLWorkbook(path))
        {
            //Read the first Sheet from Excel file.
            IXLWorksheet workSheet = workBook.Worksheet(worksheet);

            //Create a new DataTable.

            //Loop through the Worksheet rows.
            bool firstRow = true;
            foreach (IXLRow row in workSheet.Rows())
            {
                //Use the first row to add columns to DataTable.
                if (firstRow)
                {
                    foreach (IXLCell cell in row.Cells())
                    {
                        if (!string.IsNullOrEmpty(cell.Value.ToString()))
                        {
                            dt.Columns.Add(cell.Value.ToString());
                        }
                        else
                        {
                            break;
                        }
                    }
                    firstRow = false;
                }
                else
                {
                    int i = 0;
                    DataRow toInsert = dt.NewRow();
                    foreach (IXLCell cell in row.Cells(1, dt.Columns.Count))
                    {
                        try
                        {
                            toInsert[i] = cell.Value.ToString();
                        }
                        catch (Exception ex)
                        {
                           //Handle this, or don't.
                        }
                        i++;
                    }
                    dt.Rows.Add(toInsert);
                }
            }
            return dt;
        }

If you need to do any data transformations, do it while the data is in a DataTable.如果您需要进行任何数据转换,请在数据位于 DataTable 中时进行。

Then use CSVHelper to export as a CSV (SO answer I found had a solution that didn't use the Culture Info which was added as a requirement to the Library a few updates ago):然后使用 CSVHelper 导出为 CSV (所以答案我发现有一个没有使用文化信息的解决方案,该解决方案是在几个更新前作为库的要求添加的):

using CSVHelper;
using System.Globilization;
....
    public static void SaveCSV(DataTable records)
    {
        string newFile = @"C:\somePath.csv";
        using (StreamWriter writer = new StreamWriter(newFile))
        {
            using (CsvWriter csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
            {
                //add headers 
                foreach (DataColumn dc in records.Columns)
                {
                    csv.WriteField(dc.ColumnName);
                }
                csv.NextRecord();
                foreach(DataRow dr in records.Rows)
                {
                    for (int i = 0; i< records.Columns.Count; i++)
                    {
                        csv.WriteField(dr[i]);
                    }
                    csv.NextRecord();
                }
            }
        }
    }

if it's just a words data no images then you can copy data from excel and save it in another file in如果它只是一个文字数据没有图像那么你可以从 excel 复制数据并将其保存在另一个文件中

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

相关问题 保存到现有的 excel 文件而不打开新的 excel 文件 - save to existing excel file without opening new excel file 使用C#和Excel 2010打开时如何在不获取格式警告的情况下保存Excel文件 - How do I save an Excel file without getting a format warning when opening, using C# and Excel 2010 禁止Excel在打开.csv文件时自动设置列格式 - Disallow Excel to auto format columns while opening a .csv file 将 appsettings.json 保存在 .net core 3.1 中 - Save appsettings.json in .net core 3.1 为什么数据不从.Net Core 3.1 导出到 Angular 10 中的 csv 文件? - Why data not exporting to csv file in Angular 10 from .Net Core 3.1? 如何使用流,生成 csv 文件,使用 .NET Core 3.1 中的 CsvHelper? - How to use streams, to generate csv file, using CsvHelper in .NET Core 3.1? Append 到 .NET Core 3.1 中 Azure blob 文件的末尾,无需下载整个文件 - Append to the end of Azure blob file in .NET Core 3.1 without downloading the entire file .Net Core 3.1 MVC Ajax 文件上传(不使用表单对象) - .Net Core 3.1 MVC Ajax File Upload (without using a form object) 如何在没有 EF Core 的情况下在 ASP .NET Core 3.1 中使用 Identity? - How to use Identity in ASP .NET Core 3.1 without EF Core? 使用Excel Interop打开巨大的.csv文件 - Opening a huge .csv file with Excel Interop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM