【问题标题】:Export DataGrid into CSV or Excel将 DataGrid 导出为 CSV 或 Excel
【发布时间】:2013-06-06 08:47:04
【问题描述】:

如何将我的 DataGrid 信息导出到 .csv 或 excel 文件中? 我正在使用带有 .NET Framework 4.0 的 Microsoft Visual Studio 2010。

【问题讨论】:

    标签: c# wpf excel csv datagrid


    【解决方案1】:

    试试下面的示例

    private void btnexport_Click(object sender, RoutedEventArgs e)
    {
        ExportToExcel<Employee, Employees> s = new ExportToExcel<Employee, Employees>();
        s.dataToPrint = (Employees)dgEmployee.ItemsSource;
        s.GenerateReport();
    }
    
    
    
    
    
    
    /// <summary>
    /// Class for generator of Excel file
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="U"></typeparam>
    public class ExportToExcel<T, U>
        where T : class
        where U : List<T>
    {
        public List<T> dataToPrint;
        // Excel object references.
        private Excel.Application _excelApp = null;
        private Excel.Workbooks _books = null;
        private Excel._Workbook _book = null;
        private Excel.Sheets _sheets = null;
        private Excel._Worksheet _sheet = null;
        private Excel.Range _range = null;
        private Excel.Font _font = null;
        // Optional argument variable
        private object _optionalValue = Missing.Value;
    
        /// <summary>
        /// Generate report and sub functions
        /// </summary>
        public void GenerateReport()
        {
            try
            {
                if (dataToPrint != null)
                {
                    if (dataToPrint.Count != 0)
                    {
                        Mouse.SetCursor(Cursors.Wait);
                        CreateExcelRef();
                        FillSheet();
                        OpenReport();
                        Mouse.SetCursor(Cursors.Arrow);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error while generating Excel report");
            }
            finally
            {
                ReleaseObject(_sheet);
                ReleaseObject(_sheets);
                ReleaseObject(_book);
                ReleaseObject(_books);
                ReleaseObject(_excelApp);
            }
        }
        /// <summary>
        /// Make Microsoft Excel application visible
        /// </summary>
        private void OpenReport()
        {
            _excelApp.Visible = true;
        }
        /// <summary>
        /// Populate the Excel sheet
        /// </summary>
        private void FillSheet()
        {
            object[] header = CreateHeader();
            WriteData(header);
        }
        /// <summary>
        /// Write data into the Excel sheet
        /// </summary>
        /// <param name="header"></param>
        private void WriteData(object[] header)
        {
            object[,] objData = new object[dataToPrint.Count, header.Length];
    
            for (int j = 0; j < dataToPrint.Count; j++)
            {
                var item = dataToPrint[j];
                for (int i = 0; i < header.Length; i++)
                {
                    var y = typeof(T).InvokeMember
            (header[i].ToString(), BindingFlags.GetProperty, null, item, null);
                    objData[j, i] = (y == null) ? "" : y.ToString();
                }
            }
            AddExcelRows("A2", dataToPrint.Count, header.Length, objData);
            AutoFitColumns("A1", dataToPrint.Count + 1, header.Length);
        }
        /// <summary>
        /// Method to make columns auto fit according to data
        /// </summary>
        /// <param name="startRange"></param>
        /// <param name="rowCount"></param>
        /// <param name="colCount"></param>
        private void AutoFitColumns(string startRange, int rowCount, int colCount)
        {
            _range = _sheet.get_Range(startRange, _optionalValue);
            _range = _range.get_Resize(rowCount, colCount);
            _range.Columns.AutoFit();
        }
        /// <summary>
        /// Create header from the properties
        /// </summary>
        /// <returns></returns>
        private object[] CreateHeader()
        {
            PropertyInfo[] headerInfo = typeof(T).GetProperties();
    
            // Create an array for the headers and add it to the
            // worksheet starting at cell A1.
            List<object> objHeaders = new List<object>();
            for (int n = 0; n < headerInfo.Length; n++)
            {
                objHeaders.Add(headerInfo[n].Name);
            }
    
            var headerToAdd = objHeaders.ToArray();
            AddExcelRows("A1", 1, headerToAdd.Length, headerToAdd);
            SetHeaderStyle();
    
            return headerToAdd;
        }
        /// <summary>
        /// Set Header style as bold
        /// </summary>
        private void SetHeaderStyle()
        {
            _font = _range.Font;
            _font.Bold = true;
        }
        /// <summary>
        /// Method to add an excel rows
        /// </summary>
        /// <param name="startRange"></param>
        /// <param name="rowCount"></param>
        /// <param name="colCount"></param>
        /// <param name="values"></param>
        private void AddExcelRows
        (string startRange, int rowCount, int colCount, object values)
        {
            _range = _sheet.get_Range(startRange, _optionalValue);
            _range = _range.get_Resize(rowCount, colCount);
            _range.set_Value(_optionalValue, values);
        }       
        /// <summary>
        /// Create Excel application parameters instances
        /// </summary>
        private void CreateExcelRef()
        {
            _excelApp = new Excel.Application();
            _books = (Excel.Workbooks)_excelApp.Workbooks;
            _book = (Excel._Workbook)(_books.Add(_optionalValue));
            _sheets = (Excel.Sheets)_book.Worksheets;
            _sheet = (Excel._Worksheet)(_sheets.get_Item(1));
        }
        /// <summary>
        /// Release unused COM objects
        /// </summary>
        /// <param name="obj"></param>
        private void ReleaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
    

    【讨论】:

    • 我用我的班级邮件替换了员工,用列表替换了员工,对吗?我收到 12 个错误,提示:找不到类型或命名空间名称“Excel”(您是否缺少 using 指令或程序集引用?)
    • 你的项目中是否包含了 excel dll 是这样的。还包括使用 Excel =Microsoft.Interop.Office.Excel;在您的命名空间中。
    • 右键单击解决方案资源管理器中的引用->添加引用->Microsoft.Office.Interop.Excel,然后智能提示我将 Excel 替换为 Microsoft.Office.Interop.Excel。现在有 0 个错误......现在就试试吧 编辑:完美!非常感谢!!!
    【解决方案2】:

    您可以使用以下代码将数据表转换为 csv。该代码非常灵活,您可以更改列名并可以选择列数。

    要导出到 csv,您需要调用它

    ExportFile.ExportCSV(dt, "id,name", "Product ID,Name","order.csv");
    

    下面是代码:

    public class ExporFile
    {
        /// <summary>
        /// Export to CSV
        /// </summary>
        /// <param name="exportTable">Export table</param>
        /// <param name="showColumns">Columns needs to show in CSV</param>
        /// <param name="changedColumnName">Changed Column Names in CSV</param>
        /// <param name="fileName">File Name</param>
        public static void ExportCSV(DataTable exportTable, string showColumns, string changedColumnName, string fileName)
        {
            DataTable filterTable = FilterColumn(exportTable, showColumns, changedColumnName);
            string dataCSV = DataTable2CSV(filterTable, "\t", "\"");
            dataCSV = System.Web.HttpContext.Current.Server.HtmlDecode(dataCSV);
            System.Web.HttpContext.Current.Response.Charset = "";
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
            System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            System.Web.HttpContext.Current.Response.Write(dataCSV);
            System.Web.HttpContext.Current.Response.Flush();
            try
            {
                System.Web.HttpContext.Current.Response.End();
            }
            catch { };
        }
    
        /// <summary>
        /// Filter Columns
        /// </summary>
        /// <param name="exportTable"></param>
        /// <param name="showColumns"></param>
        /// <param name="changedColumnName"></param>
        /// <returns></returns>
        private static DataTable FilterColumn(DataTable exportTable, string showColumns, string changedColumnName)
        {
            DataView filterDataView = exportTable.DefaultView;
            //filterDataView.Sort = "AutoID";
            DataTable filterTable = filterDataView.ToTable(false, showColumns.Split(','));
            return ChangedExportDataColumnName(filterTable, changedColumnName);
        }
    
        /// <summary>
        /// Changed Column Datatable
        /// </summary>
        /// <param name="filterTable"></param>
        /// <param name="changedColumnName"></param>
        /// <returns></returns>
        private static DataTable ChangedExportDataColumnName(DataTable filterTable, string changedColumnName)
        {
            string[] changedNames = changedColumnName.Split(',');
    
            for (int i = 0; i < changedNames.Length; i++)
            {
                if (!String.IsNullOrEmpty(changedNames[i]))
                {
                    filterTable.Columns[i].ColumnName = changedNames[i];
                }
            }
            return filterTable;
        }
    
        /// <summary>
        /// Returns a CSV string corresponding to a datatable. However the separator can be defined and hence it can be any string separated value and not only csv.
        /// </summary>
        /// <param name="table">The Datatable</param>
        /// <param name="separator">The value separator</param>
        /// <param name="circumfix">The circumfix to be used to enclose values</param>
        /// <returns></returns>
        private static String DataTable2CSV(DataTable table, string separator, string circumfix)
        {
    
            StringBuilder builder = new StringBuilder(Convert.ToString((char)65279));
            foreach (DataColumn col in table.Columns)
            {
                builder.Append(col.ColumnName).Append(separator);
            }
            builder.Remove((builder.Length - separator.Length), separator.Length);
            builder.Append(Environment.NewLine);
    
            foreach (DataRow row in table.Rows)
            {
                foreach (DataColumn col in table.Columns)
                {
                    builder.Append(circumfix).Append(row[col.ColumnName].ToString().Replace("\"", "\"\"")).Append(circumfix).Append(separator);
                }
                builder.Remove((builder.Length - separator.Length), separator.Length);
                builder.Append(Environment.NewLine);
            }
            return builder.ToString();
        }
    }
    

    【讨论】:

      【解决方案3】:

      我认为最好和最快的选择是创建一个报告并使用 reportViewer 控件为您进行导出(它可以选择将数据导出到 Excel、PDF 和 Word 文档中)

      【讨论】:

      • 这听起来很简单,但我不知道报告是如何工作的。 atm 看起来让我有点困惑。
      • 它们非常简单,因为您有一个向导可以让您创建报表的视觉外观。然后,您只需将 reportViewer 添加到应用程序并将报告分配给它。这是一个可能对您有帮助的链接msdn.microsoft.com/en-us/library/ms252067(v=vs.80).aspx
      • 谢谢你的回答,但 Jodha 的解决方案更容易理解。
      猜你喜欢
      • 2010-09-23
      • 2010-11-01
      • 1970-01-01
      • 2015-02-08
      • 2017-01-03
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      相关资源
      最近更新 更多