【问题标题】:Export datagrid to a excel window which is opening c#将数据网格导出到正在打开 c# 的 excel 窗口
【发布时间】:2014-04-18 17:17:19
【问题描述】:

我开始使用 WPF,我正在使用数据网格

我有多个数据网格。如果我第一次单击按钮(“导出”按钮),它将创建一个新的 excel 窗口并将数据导出到第一张表。然后,我更改为另一个数据网格并第二次单击一个按钮(“导出”按钮)。因此,它将在之前创建的 excel 窗口中创建一个新工作表。你能帮我改一下代码吗?

非常感谢!!

 public void Export(DataTable dt, string sheetName, string title)
    {

        Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbooks oBooks;
        Microsoft.Office.Interop.Excel.Sheets oSheets;
        Microsoft.Office.Interop.Excel.Workbook oBook;
        Microsoft.Office.Interop.Excel.Worksheet oSheet;
        Excel.Range _range = null;


        oExcel.DisplayAlerts = false;
        oExcel.Application.SheetsInNewWorkbook = 1;
        oBooks = oExcel.Workbooks;

        oBook = (Microsoft.Office.Interop.Excel.Workbook)(oExcel.Workbooks.Add(Type.Missing));
        oSheets = oBook.Worksheets;
        oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheets.get_Item(1);
        oSheet.Name = sheetName;


        Microsoft.Office.Interop.Excel.Range head = oSheet.get_Range("A1", "C1");
        head.MergeCells = true;
        head.Value2 = title;
        head.Font.Bold = true;
        head.Font.Name = "Tahoma";
        head.Font.Size = "18";
        head.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;


        List<object> objHeaders = new List<object>();
        for (int n = 0; n <= dt.Rows.Count; n++)
        {
            objHeaders.Add(dt.Columns[n].ColumnName);
        }

        var headerToAdd = objHeaders.ToArray();


        _range = oSheet.get_Range("A3", Type.Missing);
        _range = _range.get_Resize(dt.Rows.Count, dt.Columns.Count);
        _range.ColumnWidth = 30;
        _range.set_Value(Type.Missing, headerToAdd);


        Excel.Range rowHead = oSheet.get_Range("A3", "C"+dt.Columns.Count);
        rowHead.Font.Bold = true;

        rowHead.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;

        rowHead.Interior.ColorIndex = 15;
        rowHead.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;



        int row = dt.Rows.Count;
        int col = dt.Columns.Count;
        object[,] arr = new object[row, col];


        for (int r = 0; r < dt.Rows.Count; r++)
        {
            DataRow dr = dt.Rows[r];
            for (int c = 0; c < dt.Columns.Count; c++)
            {
                arr[r, c] = dr[c];
            }
        }


        int rowStart = 4;
        int columnStart = 1;

        int rowEnd = rowStart + dt.Rows.Count - 1;
        int columnEnd = dt.Columns.Count;


        Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[rowStart, columnStart];

        Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[rowEnd, columnEnd];

        Microsoft.Office.Interop.Excel.Range range = oSheet.get_Range(c1, c2);


        range.Value2 = arr;


        range.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;

        Microsoft.Office.Interop.Excel.Range c3 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[rowEnd, columnStart];
        Microsoft.Office.Interop.Excel.Range c4 = oSheet.get_Range(c1, c3);
        oSheet.get_Range(c3, c4).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

        oExcel.Visible = true;
    }      
}

【问题讨论】:

  • 您想将聚焦的数据网格数据导出到 Excel 工作表吗?你的代码有什么问题?
  • 我的代码没有问题,但我需要更改此代码以将另一个数据网格保存到之前打开的同一个 Excel 窗口中,但在另一张表中

标签: c# wpf excel datagrid


【解决方案1】:

您需要在类中使用字段Microsoft.Office.Interop.Excel.Application oExcel 来存储其值。第一次它将为空,但第二次 - 它将被打开 excel。但是你必须小心这种行为,用户可以在第二次导出之前关闭 excel。所以你需要实现 Closed 事件处理程序并清除你的字段 (similar problem)

【讨论】:

  • 我之前尝试过这样做,但它打开了一个不同的excel窗口,之前有工作表。那么,我需要改变一些不同的东西吗?
  • 我知道了,它运行了。但是当我点击第二个时,一个 Excel 窗口会丢失之前的工作表并创建两个新工作表。哪一个有新数据,另一个空白。我使用这种情况 oBook.Sheets.Add(oBook.Sheets[numberSheet-1], Type.Missing, Type.Missing, Type.Missing); oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheets.get_Item(numberSheet++);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多