C# Winform datagridview导出EXCEL

string saveFileName = "";
            //bool fileSaved = false;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xls";
            saveDialog.Filter = "Excel文件|*.xls";
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return; //被点了取消
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                return;
            }

            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
            Microsoft.Office.Interop.Excel.Range range;
            object oMis = System.Reflection.Missing.Value;

            //显示为文本格式
            range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[dataGridView1.RowCount + 1, dataGridView1.ColumnCount]];
            range.NumberFormatLocal = "@";

            //写入标题
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
            }
            //写入数值
            for (int r = 0; r < dataGridView1.Rows.Count; r++)
            {
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
                }
                System.Windows.Forms.Application.DoEvents();
            }
            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
            //if (Microsoft.Office.Interop.cmbxType.Text != "Notification")
            //{
            //    Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[ds.Tables[0].Rows.Count + 1, 2]);
            //    rg.NumberFormat = "00000000";
            //}

            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
                }

            }
            xlApp.Quit();
            GC.Collect();//强行销毁
            MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK);
时间: 2024-10-12 11:59:48

C# Winform datagridview导出EXCEL的相关文章

Winform DataGridView导出Excel

private void 导出excelToolStripMenuItem_Click(object sender, EventArgs e) { if (dgvData.RowCount > 0) { var saveFileDialog1 = new SaveFileDialog { Filter = @"Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx", RestoreDirectory = true }; if (saveFileDialog1.Sh

Winform将DataGridView导出Excel,调用打印机打印DataGridView

DataGridView导出Excel (这个需要 Microsoft.Office.Interop.Excel.dll  在网上下载就可以) public static void ExportExcel(string fileName, DataGridView myDGV) { string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = &qu

NPOI DataGridView导出EXCEL

NPOI 官方网站 http://npoi.codeplex.com/ 加载NPOI.DLL ,引用这两个命名空间 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; #region NPOI DataGridView 导出 EXCEL /// <summary> /// NPOI DataGridView 导出 EXCEL /// </summary> /// <param name="fileName"

从DataGridView导出Excel

从DataGridView导出Excel的两种情况,不多说,直接记录代码. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Data; namespace cnblogs { public static class LoadExcel { public sta

C#winform中datagridview导出Excel

只需要传入datagridview的name即可. 1 //导出Excel()方法 2 public void ToExcel(DataGridView dataGridView) 3 { 4 //实例化一个Excel.Application对象 5 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 6 7 if (excel == null)

winform导入导出excel,后台动态添加控件

思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(string extension = Path.GetExtension(fileDialog.FileName).ToLower();),并设置允许后缀文件名: 3,NPOI转datetable,遍历tatetable转成实体类列表并入库: 导出: 1, 创建提示用户保存类,SaveFileDial

DataGridView导出Excel

把DataGridView中的信息导出到Excel是<机房收费系统>的功能之一,这里主要是VS与Office的交互,方法有n多种(从这里就可以看出这个功能难不到哪里去了),现在就介绍一下我使用的一种吧,首先需要添加并引用命名空间. 1.添加:右击窗体所在的项目名即U层的项目名--添加--引用--出现下面图片,搜索Excel,在结果中选中符合版本的Microsoft.Interop.Excel 2.引用命名空间:Imports Microsoft.Office.Interop 由于多次用到了导出

c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据

准备工作就是可以分页的DataGridView,和两个按钮,一个用来导出当前页数据到Excel,一个用来导出全部数据到Excel 没有使用SaveFileDialog,但却可以弹出保存对话框来 先做导出当前页数据到Excel的 DataGridView命名为dataGridView1 1 //按下导出按钮 2 private void button7_Click(object sender, EventArgs e) 3 { 4 print(dataGridView1); 5 } 6 publi

C# DataGridView导出Excel

using Microsoft.Office.Interop.Excel; using Excel=Microsoft.Office.Interop.Excel; //这里Excel和Interop报错,请参考我的另一篇http://blog.csdn.net/xtfnpgy/article/details/41076849 public bool ExportDataGridview(DataGridView gridView,string str_path) { if(gridView.Ro