我有一个Windows窗体,它通过一系列过滤器生成报告,并且可以导出为多种格式,其中一种是Excel,但现在我们想要预定义的Excel电源视图模板,这些模板需要Excel上的表格中的数据,当我将数据导出到Excel时,它就像一个新的,但我需要它来清除并填充现有的表,当用户转到电源视图表时,他可以看到动态表,地图和一切用他的新数据来预测,
我虽然在将我的数据网格导出到XML文件时,excel文件会有一个加载事件,它将清除当前数据,并且它们会使用将具有持久目录的XML文件填充表格
我想知道是否有更好的方法来做到这一点,因为我找不到任何解决方案,谢谢
答案 0 :(得分:1)
OP:你的任务描述有点不清楚,缺乏细节,所以我给出了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 Export to MS Excel如何在实际的演示应用程序中工作(链接:http://www.shopdigit.com/PaydayNY-2014P-Pro-for-Win-P14-2-02P.htm)。
希望这可能会有所帮助。最好的问候,
答案 1 :(得分:0)
即时使用由Windows窗体生成的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);
}
}