将数据网格导出到excel窗口,该窗口正在打开c#
本文关键字:窗口 excel 数据网 数据 网格 | 更新日期: 2023-09-27 17:58:48
我从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;
}
}
您需要在类中使用字段Microsoft.Office.Interop.Excel.Application oExcel
来存储其值。第一次它将为null,但第二次它将打开excel。但你必须小心这样的行为,用户可以在第二次导出之前关闭excel。因此,您需要实现Closed事件处理程序并清除字段(类似问题)