c# 导出到Excel
2010-11-25 12:08
183 查看
VS2008导出Excel2007
引用:Microsoft.Office.Interop.Excel
代码:
调用方式:
ExportToEXCEL Exp = new ExportToEXCEL();
Exp.ExportTo(saveFileDialog1, buttonX6, dataGridView2,groupBox1,progressBar1);
2010-11-25
引用:Microsoft.Office.Interop.Excel
代码:
public class ExportToEXCEL { public void ExportTo(SaveFileDialog saveFileDialog1, DevComponents.DotNetBar.ButtonX button3, DataGridView dataGridView1, GroupBox groupBox2, ProgressBar progressBar1) { bool b1 = true; //导出数据是否出错 double n1 = 0; //进度条显示 string s1 = ""; //进度条显示 string sSaveFileName; //EXCEL文件名 saveFileDialog1.Reset(); saveFileDialog1.DefaultExt = "xlsx"; saveFileDialog1.Filter = "xlsx|*.xlsx"; saveFileDialog1.ShowDialog(); sSaveFileName = saveFileDialog1.FileName; if (string.IsNullOrEmpty(sSaveFileName) == false) { try { button3.Enabled = false; progressBar1.Enabled = true; if (dataGridView1.Rows.Count > 0) { groupBox2.Text = "请等待,正在导出数据至EXCEL... ..."; groupBox2.Refresh(); Microsoft.Office.Interop.Excel.Application Excel1 = new Microsoft.Office.Interop.Excel.Application(); Excel1.Application.Workbooks.Add(true); Excel1.Visible = false; for (int i = 0; i < dataGridView1.ColumnCount; i++) { if (dataGridView1.Columns[i].Visible == true) Excel1.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; } for (int i1 = 0; i1 < dataGridView1.RowCount; i1++) { Application.DoEvents(); n1 = (double)(i1 + 1) / dataGridView1.RowCount * 100; progressBar1.Value = (int)n1; s1 = progressBar1.Value.ToString() + "%"; groupBox2.Text = "完成百分比:" + s1; for (int i2 = 0; i2 < dataGridView1.ColumnCount; i2++) { if (dataGridView1.Columns[i2].Visible == true) Excel1.Cells[i1 + 2, i2 + 1] = "'" + dataGridView1.Rows[i1].Cells[i2].Value; } } //如果文件存在,则先删除该文件 if (System.IO.File.Exists(sSaveFileName)) { System.IO.File.Delete(sSaveFileName); } Excel1.ActiveWorkbook.Close(true, sSaveFileName, System.Reflection.Missing.Value); Excel1.Quit(); groupBox2.Text = "数据导出EXCEL完成"; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); b1 = false; } finally { if (b1 == true) MessageBox.Show("导出EXCEL文件成功完成", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information); button3.Enabled = true; progressBar1.Enabled = false; } } }
调用方式:
ExportToEXCEL Exp = new ExportToEXCEL();
Exp.ExportTo(saveFileDialog1, buttonX6, dataGridView2,groupBox1,progressBar1);
2010-11-25
相关文章推荐
- C# 将查询的结果导出到EXCEL
- 转载——c# 大批量数据导出到excel[此为个人分享] [问题点数:20分,结帖人rocket2010]
- c# 导入导出excel
- c# 导出为Excel 的例子
- C# 导出数据到Excel
- 解决C#导出excel异常来自 HRESULT:0x800A03EC的方法 .
- C#导出EXCEL(DataTable导出EXCEL)
- [转]将c#中datagridview中的数据导出到excel中
- 用C#.net编写导入导出EXCEL文件的代码。编绎运行后,错误提示: 找不到可安装的ISAM
- 【经典】C#导出Excel总结
- C#导出到Excel或PDF
- C#中关于导出GridView数据到Excel
- C# 导出Excel 格式化
- C# DataGridView 导出 Excel(根据Excel版本显示选择不同后缀格式xls或xlsx)
- 在C#中关于excel的导入和导出操作
- C#实现 EXCEL CSV导出
- C# 将GridView导出为Excel注意事项
- DataGrid导出Excel(c#)
- C# DataGridView导出到Excel