2017-03-13 60 views
-5

我在c#console輸出中有一些行和列,我想將它導出到excel.any的建議?謝謝如何將數據從c#控制檯應用程序導出到Excel

+0

你有代碼中的行嗎?或者你只是想複製粘貼控制檯的輸出? –

+0

有關您的數據的更多信息將有助於我們爲您提供幫助 - 但您可以將數據輸出到可在Excel中打開的CSV。如果你的數據在DataGrid中,這裏是一個快速的方法:http://www.dylansweb.com/2015/06/wpf-datagrid-the-easy-way-to-export-to-csv/ – PaulF

回答

0

我利用了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); 
     } 
    } 
0

我建議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); 
相關問題