c#导出EXCEL

来源:百度知道 编辑:UC知道 时间:2024/06/06 12:36:46
大家好!我做了个导出EXCEL的功能,导出的按钮代码如下:
private void toolStripButton2_Click(object sender, EventArgs e)
{
ApplicationClass MyExcel;
Workbooks MyWorkBooks;
Workbook MyWorkBook;
Worksheet MyWorkSheet;
char MyColumns;
Range MyRange;
Object[,] MyData = new Object[500, 350];
int i, j;
MyExcel = new ApplicationClass();
MyExcel.Visible = true;
if (MyExcel == null)
{
MessageBox.Show("Excel程序无法启动!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
MyWorkBooks = MyExcel.Workbooks;
MyWorkBook = MyWorkBooks.Add(Missing.Value);
MyWorkSheet = (Worksheet)MyWorkBook.Worksheets[1];
M

using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;

namespace RC.MSOffice
{
/// <summary>
/// Excel相关
/// </summary>
public class ExcelUtility
{
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dataSet">需要导出Excel的DataSet</param>
/// <param name="fileName">导出的路径</param>
public static void ExportToExcel(DataSet dataSet, string fileName)
{
if (dataSet.Tables.Count == 0)
{
throw new Exception("DataSet中没有任何可导出的表。");
}

Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
excelApplication.DisplayAlerts = false;

Microso