导出EXCEL代码(C#)
来源:互联网 发布:mac 安装软件 编辑:程序博客网 时间:2024/05/16 17:44
try
...{ string filename;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择将导出的EXCEL文件存放路径";
sfd.Filter="excel文档(*.xls)|*.xls";
//sfd.OpenFile();
sfd.ShowDialog();
if (sfd.FileName != "")
...{
if(sfd.FileName.LastIndexOf(".xls")<=0)
...{
sfd.FileName=sfd.FileName+".xls";
}
filename=sfd.FileName;
if(System.IO.File.Exists(filename))
...{
System.IO.File.Delete(filename);
}
Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
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];
DataTable dt1 = (DataTable)dataGrid2.DataSource;
float percent=0;
long rowRead=0;
if(dt1==null)
...{
MessageBox.Show("1");
return;
}
long totalCount=dt1.Rows.Count;
this.progressBar1.Visible=true;
for (int i = 0; i < dt1.Rows.Count; i++)
...{
this.progressBar1.Value=i;
for (int j = 0; j < dt1.Columns.Count; j++)
...{
if (i == 0)
...{
worksheet.Cells[1, j + 1] = dt1.Columns[j].ColumnName;
}
worksheet.Cells[i + 2, j + 1] = dt1.Rows[i][j].ToString();
}
rowRead++;
percent=((float)(100*rowRead))/totalCount;
this.progressBar1.Text= "正在导出数据["+ percent.ToString("0.00") +"%]...";
}
this.progressBar1.Visible=false;
workbook.Saved = true;
workbook.SaveCopyAs(filename);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
workbooks = null;
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
MessageBox.Show("导出Excel完成!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//button2_Click(null,null);
}
}
catch(Exception ex)
...{
MessageBox.Show("导出Excel失败!"+ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
...{ string filename;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择将导出的EXCEL文件存放路径";
sfd.Filter="excel文档(*.xls)|*.xls";
//sfd.OpenFile();
sfd.ShowDialog();
if (sfd.FileName != "")
...{
if(sfd.FileName.LastIndexOf(".xls")<=0)
...{
sfd.FileName=sfd.FileName+".xls";
}
filename=sfd.FileName;
if(System.IO.File.Exists(filename))
...{
System.IO.File.Delete(filename);
}
Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
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];
DataTable dt1 = (DataTable)dataGrid2.DataSource;
float percent=0;
long rowRead=0;
if(dt1==null)
...{
MessageBox.Show("1");
return;
}
long totalCount=dt1.Rows.Count;
this.progressBar1.Visible=true;
for (int i = 0; i < dt1.Rows.Count; i++)
...{
this.progressBar1.Value=i;
for (int j = 0; j < dt1.Columns.Count; j++)
...{
if (i == 0)
...{
worksheet.Cells[1, j + 1] = dt1.Columns[j].ColumnName;
}
worksheet.Cells[i + 2, j + 1] = dt1.Rows[i][j].ToString();
}
rowRead++;
percent=((float)(100*rowRead))/totalCount;
this.progressBar1.Text= "正在导出数据["+ percent.ToString("0.00") +"%]...";
}
this.progressBar1.Visible=false;
workbook.Saved = true;
workbook.SaveCopyAs(filename);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
workbooks = null;
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
MessageBox.Show("导出Excel完成!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//button2_Click(null,null);
}
}
catch(Exception ex)
...{
MessageBox.Show("导出Excel失败!"+ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
- 导出EXCEL代码(C#)
- 导出EXCEL代码(C#)
- 导出EXCEL代码(C#)
- 导出到excel代码
- C# 导出Excel代码
- GridView导出Excel代码
- 导出Excel 代码
- abap导出excel代码
- java 导出excel代码
- Excel导出导入代码
- poi导出excel代码
- 【代码】C#导出Excel
- 【代码】VB导出Excel
- javaPOI导出excel代码
- 导出Excel(.NET 代码)
- DataGrid导出Excel(c#)
- C#: Excel导出
- C#Excel数据导出
- (转)XP和RUP的比较
- Oracle 8i安装问题
- Rest介绍
- 如何修改boot.ini文件的pae|awe|3gb参数
- asp.net 连接myql
- 导出EXCEL代码(C#)
- RMAN中常用configure命令
- Flash占CPU 优化
- 本地化测试错误特征分析与测试方法
- 关于DELPHI框架的后续
- RMAN 动态性能视图
- BCB通用函数库(csdn上的整理)
- RMAN备份文件格式
- 提高eclipse内存