第一种方式
public static void ExportGridViewToExcel( gridView, string filename)
{
// dt = ();
SaveFileDialog sfd = new SaveFileDialog();
filename += ("yyyyMMdd") + "-" + () + ();
= filename;
= "Excel files (*xls) | *.xls";
= true;
if (() == && () != null)
{
int rowIndex = 1;
int colIndex = 0;
int colNum = ;
miss = ;
xlapp = new ();
= true;
mBooks = ();
mBook = ()(miss);
mSheet = ();
mRange = mSheet.get_Range((object)"A1", );
//.
//设置对齐方式
= ;
//设置文字自动换行
// = true;
//设置第一行高度,即标题栏
(()["1:1", ]).RowHeight = 20;
//设置数据行行高度
(()["2:" + + 1, ]).RowHeight = 16;
//设置字体大小(10号字体)
mSheet.get_Range([1, 1], [ + 1, ]). = 10;
//设置单元格边框
range1 = mSheet.get_Range([1, 1], [ + 1, ]);
= 1;
//写标题
for (int row = 1; row <= ; row++)
{
[1, row] = [row - 1].GetTextCaption();
}
try
{
for (int i = 0; i < ; i++)
{
rowIndex++;
colIndex = 0;
for (int j = 0; j < ; j++)
{
colIndex++;
[rowIndex, colIndex] = (i, [j]);
}
}
(, .xlExcel7, miss, miss, miss, miss, ,
miss, miss, miss, miss, miss);
//return true;
}
catch (Exception ex)
{
//throw new Exception();
}
finally
{
//(false, miss, miss);
();
();
(mRange);
(mSheet);
(mBook);
(mBooks);
(xlapp);
();
}
}
else
{
//return false;
}
}
ExportGridViewToExcel((), @"c:\\");
第二种方式
string strFileName = "清查表";
string strSavePath = "";
SaveFileDialog dlgSave = new SaveFileDialog();
= "导出到Microsoft Excel Document";// 对话框题目
= strFileName;
= "Microsoft Excel|*.xls";// 过滤格局
if(()== )
{
strSavePath = ;
}
if(strSavePath != "")
{
//this..
options = new ();
//(@"c:\\");
// =
(@"c:\\", options);
}