我正在尝试将我的DataGrid导出到Excel (Office2019/365)。我的方法是使用Microsoft.Office.Interop.Excel扩展,但是它会抛出一个异常,而且我了解到它只适用于Office 2013。
,所以我的问题是,有哪些扩展可以用于Office2019,可以与Microsoft.Office.Interop.Excel互换吗?如果有尽可能少的重构就太好了。
我的方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Office.Interop.Excel;
namespace My.NameSpace {
class ExcelExport {
public static void ExportDataGrid(object sender) {
DataGrid currentGrid = sender as DataGrid;
if (currentGrid != null) {
StringBuilder sbGridData = new StringBuilder();
List<string> listColumns = new List<string>();
List<DataGridColumn> listVisibleDataGridColumns = new List<DataGridColumn>();
List<string> listHeaders = new List<string>();
Microsoft.Office.Interop.Excel.Application application = null;
Workbook workbook = null;
Worksheet worksheet = null;
int rowCount = 1;
int colCount = 1;
try {
application = new Microsoft.Office.Interop.Excel.Application();
workbook = application.Workbooks.Add(Type.Missing);
worksheet = (Worksheet)workbook.Worksheets[1];
if (currentGrid.HeadersVisibility == DataGridHeadersVisibility.Column || currentGrid.HeadersVisibility == DataGridHeadersVisibility.All) {
foreach (DataGridColumn dataGridColumn in currentGrid.Columns.Where(dataGridColumn => dataGridColumn.Visibility == Visibility.Visible)) {
listVisibleDataGridColumns.Add(dataGridColumn);
if (dataGridColumn.Header != null) {
listHeaders.Add(dataGridColumn.Header.ToString());
}
worksheet.Cells[rowCount, colCount] = dataGridColumn.Header;
colCount++;
}
// IEnumerable collection = currentGrid.ItemsSource
foreach (object data in currentGrid.ItemsSource) {
listColumns.Clear();
colCount = 1;
rowCount++;
foreach (DataGridColumn dataGridColumn in listVisibleDataGridColumns) {
string strValue = string.Empty;
Binding objBinding = null;
DataGridBoundColumn dataGridBoundColumn = dataGridColumn as DataGridBoundColumn;
if (dataGridBoundColumn != null) {
objBinding = dataGridBoundColumn.Binding as Binding;
}
DataGridTemplateColumn dataGridTemplateColumn = dataGridColumn as DataGridTemplateColumn;
if (dataGridTemplateColumn != null) {
// this is a template column, let's see the underlying dependency object
DependencyObject dependencyObject = dataGridTemplateColumn.CellTemplate.LoadContent();
FrameworkElement frameworkElement = dependencyObject as FrameworkElement;
if (frameworkElement == null) {
FieldInfo fieldInfo = frameworkElement.GetType().GetField("ContentProperty", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (fieldInfo == null) {
if (frameworkElement is System.Windows.Controls.TextBox || frameworkElement is TextBlock || frameworkElement is ComboBox) {
fieldInfo = frameworkElement.GetType().GetField("TextProeprty");
} else if (frameworkElement is DatePicker) {
fieldInfo = frameworkElement.GetType().GetField("SelectedDateProperty");
}
}
if (fieldInfo != null) {
DependencyProperty dependencyProperty = fieldInfo.GetValue(null) as DependencyProperty;
if (dependencyProperty != null) {
BindingExpression bindingExpression = frameworkElement.GetBindingExpression(dependencyProperty);
if (bindingExpression != null) {
objBinding = bindingExpression.ParentBinding;
}
}
}
}
}
if (objBinding != null) {
if (!String.IsNullOrEmpty(objBinding.Path.Path)) {
PropertyInfo pi = data.GetType().GetProperty(objBinding.Path.Path);
if (pi != null) {
object propValue = pi.GetValue(data, null);
if (propValue != null) {
strValue = Convert.ToString(propValue);
} else {
strValue = string.Empty;
}
}
}
if (objBinding.Converter != null) {
if(!String.IsNullOrEmpty(strValue)) {
strValue = objBinding.Converter.Convert(strValue, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
} else {
strValue = objBinding.Converter.Convert(data, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
}
}
}
listColumns.Add(strValue);
worksheet.Cells[rowCount, colCount] = strValue;
colCount++;
}
}
}
} catch (System.Runtime.InteropServices.COMException) {
} finally {
workbook.Close();
application.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
}
}
}
}
}发布于 2021-08-19 12:37:54
它可能没有相同的api,但是EPPlus是从c#创建excel文件的好选择。
它不使用互操作库,因此它不依赖于安装的Office版本(根本不需要office)。
版本4.5.3是使用LGPL许可证的最后一个版本,以后的版本拥有双重许可证,可对商业使用收费。
https://stackoverflow.com/questions/68847809
复制相似问题