C# Winform中导出DataSet到Excel,使用保存文件对话框
首先,需要添加引用Excel.dll,Excel组件VS.Net本身是没有的,以下是生成Excel.dll的方法。
1.要保证机器本身要安装Microsoft Office。
2.把C:Program FilesMicrosoft OfficeOffice1:【office 安装路径】下的EXCEL9.OLB文件拷贝到C:Visual Studio.NetSDKv1.1Bin:【VS.Net安装路径】路径下。
3.打开Visual Studio .Net2003/2008命令提示,运行TlbImp Excel9.olb Excel.dll ,就会在[C:Visual Studio.NetSDKv1.1Bin]下生成Excel.dll组件。(Tlbimp命令是vs自带的一个工具,位于Microsoft Visual Studio 8SDKv2.0Bin下)
4.把Excel.dll复制到你的项目中,在项目中添加Excel.dll引用就可以了。(最好是用最新版本的Office生成excel.dll,否则旧版本的office的dll有可能在高版本机器上不兼容。)
以下是把一个dataset中的数据导出到excel的实例:
记得在解决方案资源管理器中添加excel.dll的引用后,在用此函数的时候在顶部 using Excel;
public void ExportExcel(DataSet ds)
{
if (ds == null) return;
string saveFileName = "";
bool fileSaved = false;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = "Sheet1";
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
return;
}
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Excel.Range range;
string oldCaption = DateTime.Today.ToString("yy-MM-dd");
long totalCount = ds.Tables[0].Rows.Count;
long rowRead = 0;
float percent = 0;
worksheet.Cells[1, 1] = DateTime.Today.ToString("yy-MM-dd");
//写入字段
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
worksheet.Cells[2, i + 1] = ds.Tables[0].Columns[i].ColumnName;
range = (Excel.Range)worksheet.Cells[2, i + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;
}
//写入数值
for (int r = 0; r < ds.Tables[0].Rows.Count; r++)
{
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
worksheet.Cells[r + 3, i + 1] = ds.Tables[0].Rows[r][i];
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
//this.lbl_process.Text = "正在导出数据[" + percent.ToString("0.00") + "%]..."; //这里可以自己做一个label用来显示进度.
System.Windows.Forms.Application.DoEvents();
}
this.lbl_process.Visible = false;
range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[ds.Tables[0].Rows.Count + 2, ds.Tables[0].Columns.Count]);
range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
if (ds.Tables[0].Columns.Count > 1)
{
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
}
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!n" + ex.Message);
}
}
else
{
fileSaved = false;
}
xlApp.Quit();
GC.Collect();//强行销毁
if (fileSaved && File.Exists(saveFileName))
{
//System.Diagnostics.Process.Start(saveFileName);
MessageBox.Show("导出成功!","通知");
}
}











