using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop;
namespace AdvancedFiltration
{
class dataSetToExcel
{
/// <summary>
/// DataSet集合输出到Excel中
/// </summary>
/// <param name="dataSet">DataSet集合</param>
/// <param name="isShowExcle">是否打开Excel文件,true or false</param>
/// <returns>,输出成功,则为true</returns>
public bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
{
DataTable dataTable = dataSet.Tables[0];
int rowNumber = dataTable.Rows.Count;//总行数
int columnNumber = dataTable.Columns.Count;//总列数
if (rowNumber == 0)
{
MessageBox.Show("没有任何数据可以导入到Excel文件!");
return false;
}
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);//新建一个工作簿
excel.Visible = isShowExcle;//是否打开该Excel文件
//填充数据
for (int c = 0; c < rowNumber; c++)
{
for (int j = 0; j < columnNumber; j++)
{
excel.Cells[c + 1, j + 1] = dataTable.Rows[c].ItemArray[j];
}
}
return true;
}
}
}
ERROR:命名空间"Microsoft"中不存在类型或命名空间名称"Office"(是否缺少程序引用?)
添加引用 —— COM —— Microsoft Office14.0 Object Library 和 Microsoft Excel 14.0 Object Library