将datagrid导出到打开c#的excel窗口

时间:2014-04-18 06:50:23

标签: c# wpf excel datagrid

我在WPF开始,我正在使用datagrid

我有多个数据网格。如果我第一次单击按钮("导出"按钮),它将创建一个新的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;
    }      
}

1 个答案:

答案 0 :(得分:0)

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