【问题标题】:Import Csv Files to Excel and replace comma that separates by Columns将 Csv 文件导入 Excel 并替换按列分隔的逗号
【发布时间】:2016-09-23 03:05:52
【问题描述】:

您好,我需要通过将逗号更改为数据之间的列,将使用 LinqToCsv 库从 linq 查询结果填充的一些 csv 文件导入 Excel 工作簿?

`//Generate CSV Files for each item in the Listview
        CsvFileDescription outpCsvFileDescription = new CsvFileDescription
        {
            SeparatorChar = ',',
            FirstLineHasColumnNames = true
        };

        for (int i = 0; i < listView.Items.Count; i++)
        {
            dynamic currentItemInLoop = listView.Items[i];
            //On est obligé de cast cette variable en String pour qu'on puisse l'utiliser dans le LinQ
            //currentItemInLoop.nameAttribute => MainWindow.xaml -> ListView x:Name="listView" -> Columns...
            String frs = (String)currentItemInLoop.Name;
            DateTime myDate = (DateTime) currentItemInLoop.TransactionDate;
            var infoEcheances = from f in db.F_ECHEANCES
                                join c in db.F_LECRITURES on f.ECH_No equals c.ECH_No
                                where
                    f.ECH_Intitule == frs &&
                    EntityFunctions.TruncateTime(f.cbModification) ==
                    EntityFunctions.TruncateTime(DatePicker.SelectedDate)
                select
                    new 
                    {
                        f.ECH_DateEch,
                        f.ECH_RefPiece,
                        f.ECH_Libelle,
                        c.LEC_Montant,
                        f.ECH_Montant
                    };

            CsvContext cc = new CsvContext();
            string myPath = @"C:\Users\DefaultAccount\Desktop\Projet Top Of Travel\FichiersCSV\";
            string filename = string.Format(frs);
            filename = Regex.Replace(filename + " " + myDate, @"[^0-9a-zA-Z]", " ");
            string finalPath = System.IO.Path.Combine(myPath, filename + ".csv");
            cc.Write(infoEcheances, finalPath, outpCsvFileDescription);`

【问题讨论】:

  • Excel 不会打开 CSV 文件,它会使用本地化默认值导入它们。在某些国家/地区,默认分隔符是;。您可以通过转到“数据”菜单使用不同的设置。为避免这种情况,请使用 EPPLus 或类似库创建xlsx 文件,而不是创建 CSV。另一种选择是简单地使用您想要的分隔符创建文件。

标签: c# excel linq csv


【解决方案1】:

您可以使用 COM 从 c# 直接将其导入 Excel。

using Excel = Microsoft.Office.Interop.Excel; 

public class ExcelReports
{
    public Excel.Application excelApp;
    public Excel.Workbook excelWorkbook;
    public Excel.Worksheet excelWorksheet;
    public int row = 1;
    public int col = 1;

    public ExcelReports( String fullName )
    {
        excelApp = new Excel.Application();

        Excel.Workbook newWorkbook = excelApp.Workbooks.Add();
        excelWorkbook = excelApp.ActiveWorkbook;

        excelWorksheet = (Excel.Worksheet)excelWorkbook.Worksheets.Add();
        excelWorksheet.Cells.ClearContents();
        excelWorksheet.Cells[row, col]  = "hello";
  }

}

设置单元格的代码....从我的代码中删除...您可以将其更改为从 yr 数组中输出每一行。它可能需要按摩才能起作用。网上有很多广泛的例子。有一些优化可以加快这项工作。

Excel 还将导入 CSV 文件并允许您对分隔符等进行一些控制,但此过程是手动的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2016-08-18
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多