WPF中DataGrid的数据导出到Excel

public static void ZdExportToExcel(System.Windows.Controls.DataGrid dg, string name, MetroWindow frm)
        {
           
            dg.SelectAllCells();
 
            dg.ClipboardCopyMode = System.Windows.Controls.DataGridClipboardCopyMode.IncludeHeader;
 
            ApplicationCommands.Copy.Execute(null, dg);
 
            string result = (string)Clipboard.GetData(DataFormats.Text);
 
            string fileName = AppDomain.CurrentDomain.BaseDirectory+name+ ".xls";
 
            dg.UnselectAllCells();
 
            StreamWriter swr = new StreamWriter(fileName, false, Encoding.GetEncoding("gb2312"));
 
            swr.WriteLine(result.Replace(',', ' '));
 
            swr.Close();
 
 
        }
 public static void ExportToExcel(System.Windows.Controls.DataGrid dg, string name, MetroWindow frm)
        {
            dg.SelectAllCells();
 
            dg.ClipboardCopyMode = System.Windows.Controls.DataGridClipboardCopyMode.IncludeHeader;
 
            ApplicationCommands.Copy.Execute(null, dg);
 
            string result = (string)Clipboard.GetData(DataFormats.Text);
 
            SaveFileDialog sfd = new SaveFileDialog();
 
            sfd.FileName = name;
 
            sfd.Filter = "Excel文件(*.xls)|*.xls|Csc文件(*.csv)|*.csv|所有文件(*.*)|*.*";
 
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string path = System.IO.Path.GetDirectoryName(sfd.FileName);
 
                dg.UnselectAllCells();
 
                StreamWriter swr = new StreamWriter(sfd.FileName, false, Encoding.GetEncoding("gb2312"));
 
                swr.WriteLine(result.Replace(',', ' '));
 
                swr.Close();
            }
        }
wpf,可以使用Microsoft.Office.Interop.Excel库来实现将DataGrid数据导出Excel文件的功能。首先,我们需要创建一个新的Excel应用程序实例,并且添加一个工作簿和工作表。接着,我们可以利用DataGrid数据来填充Excel表格,并设置单元格的格式和样式。最后,我们需要保存这个工作簿为一个Excel文件,并关闭Excel应用程序实例。以下是一个简单的示例代码: ```csharp private void ExportToExcel(DataGrid dataGrid, string filePath) { Microsoft.Office.Interop.Excel._Application excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook workbook = excelApp.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel._Worksheet worksheet = null; try { // 获取DataGrid的列数和行数 int columnsCount = dataGrid.Columns.Count; int rowsCount = dataGrid.Items.Count; worksheet = workbook.ActiveSheet; // 将DataGrid数据填充到Excel表格 for (int i = 0; i < columnsCount; i++) { worksheet.Cells[1, i + 1] = dataGrid.Columns[i].Header; for (int j = 0; j < rowsCount; j++) { worksheet.Cells[j + 2, i + 1] = ((TextBlock)dataGrid.Columns[i].GetCellContent(dataGrid.Items[j])).Text; } } // 保存工作簿为Excel文件 workbook.SaveAs(filePath); } catch (Exception ex) { // 处理异常 } finally { // 关闭Excel应用程序实例 excelApp.Quit(); workbook = null; worksheet = null; } } ``` 需要注意的是,由于这里使用了Interop库,所以需要确保目标机器上安装了Excel应用程序。另外,导出数据量较大时需要注意性能问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值