导出Windows窗体datagridview,以Excel的function数据透视表与C#

我有一个窗体,通过一系列的filter生成报告,它可以导出到多种格式,其中之一Excel,但现在我们想要有预定义的Excel电源视图模板,需要从Excel表上的数据,当我导出我的数据到Excel就像一个新的,但我需要它清除和填充现有的表,他们当用户去权力的观点表,他可以看到dynamic表,地图,以及所有预先,用他的新数据

我虽然在导出我的数据网格到一个XML文件和Excel文件将有一个负载事件,将清除当前的数据,他们用XML文件,将具有永久目录

我想知道是否有更好的方法来做到这一点,因为我无法find任何解决scheme,谢谢

OP:你的任务描述有点不清楚,缺less细节,所以我将DataGrid内容导出到Microsoft Excel过程的这个例子:

 public bool Export2Excel(DataTable dataTable) { object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application _appExcel = null; Microsoft.Office.Interop.Excel.Workbook _excelWorkbook = null; Microsoft.Office.Interop.Excel.Worksheet _excelWorksheet = null; try { if (dataTable.Rows.Count <= 0) { throw new ArgumentNullException("Table is Empty"); } // excel app object _appExcel = new Microsoft.Office.Interop.Excel.Application(); // excel workbook object added to app _excelWorkbook = _appExcel.Workbooks.Add(misValue); _excelWorksheet = _appExcel.ActiveWorkbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet; // column names row (range obj) Microsoft.Office.Interop.Excel.Range _columnsNameRange; _columnsNameRange = _excelWorksheet.get_Range("A1", misValue).get_Resize(1, dataTable.Columns.Count); // column names array to be assigned to _columnNameRange string[] _arrColumnNames = new string[dataTable.Columns.Count]; // set Excel columns NumberFormat property // note; most important for decimal-currency, DateTime for (int i = 0; i < dataTable.Columns.Count; i++) { // array of column names _arrColumnNames[i] = dataTable.Columns[i].ColumnName; string _strType = dataTable.Columns[i].DataType.FullName.ToString(); switch (_strType) { case "System.DateTime": { _excelWorksheet.Range["A1"].Offset[misValue, i].EntireColumn.NumberFormat = "MM/DD/YY"; break; } case "System.Decimal": { _excelWorksheet.Columns["A"].Offset[misValue, i].EntireColumn.NumberFormat = "$ #,###.00"; break; } case "System.Double": { _excelWorksheet.Columns["A"].Offset[misValue, i].EntireColumn.NumberFormat = "#.#"; break; } case "System.Int8": case "System.Int16": case "System.Int32": case "System.Int64": { // use general format for int //_excelWorksheet.Columns["A"].Offset[misValue, i].EntireColumn.NumberFormat = "####"; break; } default: break; } } // assign array to column headers range, make 'em bold _columnsNameRange.set_Value(misValue, _arrColumnNames); _columnsNameRange.Font.Bold = true; // populate data content row by row for (int Idx = 0; Idx < dataTable.Rows.Count; Idx++) { _excelWorksheet.Range["A2"].Offset[Idx].Resize[1, dataTable.Columns.Count].Value = dataTable.Rows[Idx].ItemArray; } // Autofit all Columns in the range _columnsNameRange.Columns.EntireColumn.AutoFit(); // quit excel app process if (_appExcel != null) { _appExcel.UserControl = false; _appExcel.Quit(); } return true; } catch { throw; } finally { _excelWorksheet = null; _excelWorkbook = null; _appExcel = null; misValue = null; } } #endregion 

}

你可以为你的目的定制它。

此外,您可以看到DataGrid导出到MS Excel工作在实际的演示应用程序(链接: http : //www.shopdigit.com/PaydayNY-2014P-Pro-for-Win-P14-2-02P.htm )。

希望这可能有帮助。 最好的祝福,

即时通讯使用由窗体生成的XML,链接到我的Excel表格作为源

使用这种方法

 public static void SerializeObject(this List<SPSR_ReporteSabanasxOrdenes_Todos_Result> list, string fileName) { var serializer = new XmlSerializer(typeof(List<SPSR_ReporteSabanasxOrdenes_Todos_Result>)); using (var stream = File.OpenWrite(fileName)) { serializer.Serialize(stream, list); } }