不用重写,//创建对象
Microsoft.Office.Interop.Excel.Application ecl = new Microsoft.Office.Interop.Excel.Application();
ecl.Workbooks.Add(true);//允许工作区域放东西 ecl.Cells[1, 1] = "姓名";
ecl.Cells[1, 2] = "学号";
ecl.Cells[1, 3] = "性别";
ecl.Cells[1, 4] = "年龄";
ecl.Cells[1, 5] = "座位";
ecl.Cells[1, 6] = "住址"; //////////////////////
for (int i = 2; i < ds.Tables["stuInfo"].Rows.Count + 2; i++)
{
ecl.Cells[i, 1] = ds.Tables["stuInfo"].Rows[i - 2][0].ToString();
ecl.Cells[i, 2] = ds.Tables["stuInfo"].Rows[i - 2][1].ToString();
ecl.Cells[i, 3] = ds.Tables["stuInfo"].Rows[i - 2][2].ToString();
ecl.Cells[i, 4] = ds.Tables["stuInfo"].Rows[i - 2][3].ToString();
ecl.Cells[i, 5] = ds.Tables["stuInfo"].Rows[i - 2][4].ToString();
ecl.Cells[i, 6] = ds.Tables["stuInfo"].Rows[i - 2][5].ToString();
} ecl.Visible = true;//显示EXCEL可以看出每个单元格都是独立的所以直接ecl.Cells[?,?]=?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Reflection; // 引用这个才能使用Missing字段
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelDivision
{
///
/// 编辑类
/// 创建excel工作薄工作表以及写入数据
///
public class ExcelEdit
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public Excel.Application app;
public Excel.Workbooks wbs;
public Excel.Workbook wb;
///
/// 构造函数,不创建Excel工作薄
///
public ExcelEdit()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///
/// 创建Excel工作薄
///
public void Create()//创建一个Excel对象
{
try
{
app = new Excel.Application(); wbs = app.Workbooks;
wb = wbs.Add(true);
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
///
/// 显示Excel
///
public void ShowExcel()
{
app.Visible = true;
}
///
/// 获取一个工作表
///
///
///
public Excel.Worksheet GetSheet(string SheetName)
{
Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[SheetName];
GC.Collect();
return s;
}
///
/// 添加一个工作表
///
///
///
public Excel.Worksheet AddSheet(string SheetName)
{
Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
try
{
s.Name = SheetName;
GC.Collect();
return s;
}
catch
{
return null;
}
}
///
/// 删除一个工作表
///
///
public void DelSheet(string SheetName)
{
try
{
((Excel.Worksheet)wb.Worksheets[SheetName]).Delete();
GC.Collect();
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
///
/// 重命名一个工作表
///
///
///
///
public Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)
{
Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[OldSheetName];
try
{
s.Name = NewSheetName;
GC.Collect();
return s;
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
return null;
}
}
///
/// 将指定字符串写入指定单元格中
///
/// 要写入的数据
/// 工作表名称
/// 第几行
/// 第几列
public void WriteData(string data, string sheetName, int row, int column)
{
try
{
Excel.Worksheet worksheet = GetSheet(sheetName);
worksheet.Activate();
app.Cells[row, column] = data;
/*
* 7) 设置指定列的宽度(单位:字符个数),以第一列为例:
* ExcelApp.ActiveSheet.Columns[1].ColumnsWidth := 5;
*
* 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
* ExcelApp.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米
*/
GC.Collect();
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
}
} 不知道这个对你有用没有