C#导出Excel文件Demo(Asp.Net也可用)

来源:互联网 发布:外网端口查询 编辑:程序博客网 时间:2024/05/21 17:43

    以前导出Execel一直用org.in2bits.MyXls.dll 最近又用到,稍微加了点验证和其他的代码,这里作下记录。、

            (首先要下载org.in2bits.MyXls.dll 文件并引用到项目中)

           下载地址:http://download.csdn.net/detail/hw1233456/5991201

            相关代码:

            if (this.dgvData.Rows.Count == 0)

            {
                MessageBox.Show("没有信息,无法导出文件!","系统提示");
                return;
            }
            int state = 0;
            DialogResult dr = folderBrowserDialog1.ShowDialog(this);
            string filePath = folderBrowserDialog1.SelectedPath;
            if (string.IsNullOrEmpty(filePath))
            {
                MessageBox.Show("请指定导出路径!");
                return;
            }
            org.in2bits.MyXls.XlsDocument doc = new org.in2bits.MyXls.XlsDocument();
            doc.FileName = DateTime.Now.ToString("yyyyMMdd").Replace("-", "").Replace(":", "").Replace(" ", "") + ".xls";//excel文件名称
            if (System.IO.File.Exists(@filePath + "\\" + doc.FileName))
            {
                if (MessageBox.Show("文件已存在,是否覆盖?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                {
                    return;
                }
                else
                {
                    System.IO.File.Delete(@filePath + "\\" + doc.FileName);
                }
            }
            org.in2bits.MyXls.Worksheet sheet = doc.Workbook.Worksheets.AddNamed("局计划过磅化验过磅单数据Excel表");//Excel工作表名称
            org.in2bits.MyXls.Cells cells = sheet.Cells;
            int colnum = this.dgvData.Columns.Count; //获取列数 
            for (int i = 0; i < colnum; i++)
            {
                string headerText = this.dgvData.Columns[i].HeaderText;
                cells.Add(1, (i + 1), headerText);//导出gridView列名
            }
            for (int i = 0; i < dgvData.Rows.Count; i++)
            {
                for (int j = 0; j < colnum; j++)
                {
                    string text = dgvData.Rows[i].Cells[j].Value == null ? "" : dgvData.Rows[i].Cells[j].Value.ToString();//.Trim().Replace("&nbsp;", "").Replace("&#160;", "");
                    cells.Add((i + 2), (j + 1), text);
                }
            }
            try
            {
                doc.Save(@filePath);  //保存到指定位置
                state = 1;
                MessageBox.Show("导出成功!");
                System.Diagnostics.Process.Start(@filePath + "\\" + doc.FileName);  
                //Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                //excel.Application.Workbooks.Add(@filePath + "\\" + doc.FileName);
                //excel.Visible = true;
            }
            catch
            {
                if (state == 1)
                {
                    MessageBox.Show("打开文件失败!");
                }
                else
                {
                    MessageBox.Show("导出失败!");
                }
            }
            //doc.Send();//把写好的excel文件输出到客户端(B/S)
原创粉丝点击