方法一
Microsoft
SaveFileDialog savefiledialog = new SaveFileDialog();
System
appexcel = new Microsoft
Microsoft
Microsoft
Microsoft
//設置對象不可見
appexcel
System
System
workbookdata = appexcel
worksheetdata = (Microsoft
//給工作表賦名稱
worksheetdata
for (int i =
{
worksheetdata
}
//因為第一行已經寫了表頭
rangedata = worksheetdata
Microsoft
//irowcount為實際行數
int irowcount = dt
int iparstedrow =
//ieachsize為每次寫行的數值
int ieachsize =
//icolumnaccount為實際列數
int icolumnaccount = dt
//在內存中聲明一個ieachsize×icolumnaccount的數組
object[
icurrsize = ieachsize;
while (iparstedrow < irowcount)
{
if ((irowcount
icurrsize = irowcount
//用for循環給數組賦值
for (int i =
{
for (int j =
objval[i
System
}
string X =
string col =
if (icolumnaccount <=
{
col = ((char)(
}
else
{
col = ((char)(
}
xlrang = worksheetdata
// 調用range的value
xlrang
iparstedrow = iparstedrow + icurrsize;
}
//保存工作表
System
xlrang = null;
//調用方法關閉excel進程
appexcel
方法二
using System
private void dataTableToCsv(DataTable table
{
string title =
FileStream fs = new FileStream(file
//FileStream fs
StreamWriter sw = new StreamWriter(new BufferedStream(fs)
for (int i =
{
title += table
}
title = title
sw
foreach (DataRow row in table
{
string line =
for (int i =
{
line += row[i]
}
line = line
sw
}
sw
fs
}
dataTableToCsv(dt
System
方法三:(可以自己調整單元格的格式
try
{
//沒有數據的話就不往下執行
if (dataGridView
return;
//實例化一個Excel
Microsoft
//讓後台執行設置為不可見
//excel
excel
//新增加一個工作簿
excel
//生成Excel中列頭名稱
for (int i =
{
excel
}
//把DataGridView當前頁的數據保存在Excel中
for (int i =
{
for (int j =
{
if (dataGridView
{
excel
}
else
{
excel
}
}
}
//設置禁止彈出保存和覆蓋的詢問提示框
excel
excel
////保存工作簿
//excel
////保存excel文件
//excel
////確保Excel進程關閉
//excel
//excel = null;
}
catch (Exception ex)
{
MessageBox
}
Excel
From:http://tw.wingwit.com/Article/program/net/201311/11464.html