简体   繁体   English

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

[英]Export datagrid to a excel window which is opening c#

I'm begining in WPF and i'm working with datagrid 我开始使用WPF,并且正在使用datagrid

I have multi datagrids. 我有多个数据网格。 If i click a button ("Export" button) first time, it will create a new excel window and export data to first sheet. 如果我第一次单击按钮(“导出”按钮),它将创建一个新的Excel窗口并将数据导出到第一张工作表。 Then, i change to another datagrid and click a button ("Export" button) second time. 然后,我更改为另一个数据网格,然后第二次单击一个按钮(“导出”按钮)。 so, it will create a new sheet in excel window which created before. 因此,它将在之前创建的excel窗口中创建一个新工作表。 Can you help me change my code ? 您能帮我更改密码吗?

Thank you very much!! 非常感谢你!!

 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;
    }      
}

You need to use field Microsoft.Office.Interop.Excel.Application oExcel in your class to store its value. 您需要在类中使用Microsoft.Office.Interop.Excel.Application oExcel字段来存储其值。 First time it will be null, but on second - it will be opened excel. 第一次,它将为null,但是第二次-将打开excel。 But you must be carefull with such a behavior, user can close excel, before 2nd export. 但是您必须谨慎对待这种行为,用户可以在第二次导出之前关闭excel。 So you need implement Closed event handler and clear your field ( similar problem ) 因此,您需要实现Closed事件处理程序并清除您的字段( 类似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM