

































































































































首先需要添加一个引用:
文件开头增加:
using Microsoft.Office.Interop.Excel;
private void readBt_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application app;
Workbooks wbs;
Worksheet ws;
{
Microsoft.Office.Interop.Excel.Application app;
Workbooks wbs;
Worksheet ws;
app = new Microsoft.Office.Interop.Excel.Application();
wbs = app.Workbooks;
wbs.Add(@"d:/DataTableToExcel.xls");
ws = (Worksheet)app.Worksheets.get_Item(2);
int a = ws.Rows.Count;
int b = ws.Columns.Count;
MessageBox.Show(a.ToString() + "|" + b.ToString());
//for (int i = 1; i < 3; i++)
{
for (int j = 1; j <= 15; j++)
{
Range range = ws.get_Range(app.Cells[j, 1], app.Cells[j, 1]);
range.Select();
MessageBox.Show(app.ActiveCell.Text.ToString());
if (app.ActiveCell.Text.Equals("")) break;
range = ws.get_Range(app.Cells[j, 2], app.Cells[j, 2]);
range.Select();
MessageBox.Show(app.ActiveCell.Text.ToString());
}
}
wbs = app.Workbooks;
wbs.Add(@"d:/DataTableToExcel.xls");
ws = (Worksheet)app.Worksheets.get_Item(2);
int a = ws.Rows.Count;
int b = ws.Columns.Count;
MessageBox.Show(a.ToString() + "|" + b.ToString());
//for (int i = 1; i < 3; i++)
{
for (int j = 1; j <= 15; j++)
{
Range range = ws.get_Range(app.Cells[j, 1], app.Cells[j, 1]);
range.Select();
MessageBox.Show(app.ActiveCell.Text.ToString());
if (app.ActiveCell.Text.Equals("")) break;
range = ws.get_Range(app.Cells[j, 2], app.Cells[j, 2]);
range.Select();
MessageBox.Show(app.ActiveCell.Text.ToString());
}
}
}
private void writeBt_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.ApplicationClass apc = new ApplicationClass();
apc.Visible = false;
Workbook wkbook = apc.Workbooks.Add(true);
Worksheet wksheet = (Worksheet)wkbook.ActiveSheet;
int rowIndex = 2;
int colIndex = 1;
{
Microsoft.Office.Interop.Excel.ApplicationClass apc = new ApplicationClass();
apc.Visible = false;
Workbook wkbook = apc.Workbooks.Add(true);
Worksheet wksheet = (Worksheet)wkbook.ActiveSheet;
int rowIndex = 2;
int colIndex = 1;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[5, 3]).NumberFormat = "@";
//取得列标题
for(int i=1;i<=3;i++)
{
wksheet.Cells[1, i] = "C" + i.ToString();
}
//写入行
for (int i = 1; i <= 5; i++)
{
apc.Cells[i, 1] = "L" + i.ToString();
apc.Cells[i, 2] = "L" + i.ToString();
apc.Cells[i, 3] = "L" + i.ToString();
wksheet.get_Range(apc.Cells[i, 1], apc.Cells[i, 3]).HorizontalAlignment = XlHAlign.xlHAlignLeft;
wksheet.get_Range(apc.Cells[i, 2], apc.Cells[i, 3]).HorizontalAlignment = XlHAlign.xlHAlignLeft;
wksheet.get_Range(apc.Cells[i, 3], apc.Cells[i, 3]).HorizontalAlignment = XlHAlign.xlHAlignLeft;
}
//取得列标题
for(int i=1;i<=3;i++)
{
wksheet.Cells[1, i] = "C" + i.ToString();
}
//写入行
for (int i = 1; i <= 5; i++)
{
apc.Cells[i, 1] = "L" + i.ToString();
apc.Cells[i, 2] = "L" + i.ToString();
apc.Cells[i, 3] = "L" + i.ToString();
wksheet.get_Range(apc.Cells[i, 1], apc.Cells[i, 3]).HorizontalAlignment = XlHAlign.xlHAlignLeft;
wksheet.get_Range(apc.Cells[i, 2], apc.Cells[i, 3]).HorizontalAlignment = XlHAlign.xlHAlignLeft;
wksheet.get_Range(apc.Cells[i, 3], apc.Cells[i, 3]).HorizontalAlignment = XlHAlign.xlHAlignLeft;
}
//设置表格样式
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[1, 3]).Interior.ColorIndex = 20;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[1, 3]).Font.ColorIndex = 3;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[1, 3]).Borders.Weight = XlBorderWeight.xlThin;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[5, 3]).Columns.AutoFit();
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[1, 3]).Interior.ColorIndex = 20;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[1, 3]).Font.ColorIndex = 3;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[1, 3]).Borders.Weight = XlBorderWeight.xlThin;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[5, 3]).Columns.AutoFit();
if (File.Exists(@"d:/test.xls"))
{
File.Delete(@"d:/test.xls");
}
{
File.Delete(@"d:/test.xls");
}
wkbook.SaveAs(@"d:/test.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wkbook.Close(Type.Missing, Type.Missing, Type.Missing);
apc.Quit();
wkbook = null;
apc = null;
apc.Quit();
wkbook = null;
apc = null;
}