C#vs的windows窗体的datagridview数据的导出

C#vs的windows窗体的datagridview数据的导出

效果

导出1
导出2

代码

1.在导出事件(daochu_Click)中编辑代码:

private void daochu_Click(object sender, EventArgs e)
        {
            string fileName = "";

            string saveFileName = "";

            SaveFileDialog saveDialog = new SaveFileDialog();

            saveDialog.DefaultExt = "xlsx";

            saveDialog.Filter = "Excel文件|*.xlsx";

            saveDialog.FileName = fileName;

            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 

            string yinhao = "'";
            //写入标题             
            worksheet.Cells[1, 1] = yinhao + dataGridView1.Columns[1].HeaderText.ToString().Trim();
            worksheet.Cells[1, 2] = yinhao + dataGridView1.Columns[2].HeaderText.ToString().Trim();
            worksheet.Cells[1, 3] = yinhao + dataGridView1.Columns[3].HeaderText.ToString().Trim();
            worksheet.Cells[1, 4] = yinhao + dataGridView1.Columns[4].HeaderText.ToString().Trim();
            worksheet.Cells[1, 5] = yinhao + dataGridView1.Columns[5].HeaderText.ToString().Trim();
            worksheet.Cells[1, 6] = yinhao + dataGridView1.Columns[6].HeaderText.ToString().Trim();
            worksheet.Cells[1, 7] = yinhao + dataGridView1.Columns[7].HeaderText.ToString().Trim();
            worksheet.Cells[1, 8] = yinhao + dataGridView1.Columns[8].HeaderText.ToString().Trim();
            worksheet.Cells[1, 9] = yinhao + dataGridView1.Columns[9].HeaderText.ToString().Trim();
            worksheet.Cells[1, 10] = yinhao + dataGridView1.Columns[10].HeaderText.ToString().Trim();
            //写入数值

            for (int r = 0; r < dataGridView1.Rows.Count; r++)
            {

                worksheet.Cells[r + 2, 1] = yinhao + dataGridView1.Rows[r].Cells[1].Value.ToString().Trim();
                worksheet.Cells[r + 2, 2] = yinhao + dataGridView1.Rows[r].Cells[2].Value.ToString().Trim();
                worksheet.Cells[r + 2, 3] = yinhao + dataGridView1.Rows[r].Cells[3].Value.ToString().Trim();
                worksheet.Cells[r + 2, 4] = yinhao + dataGridView1.Rows[r].Cells[4].Value.ToString().Trim();
                worksheet.Cells[r + 2, 5] = yinhao + dataGridView1.Rows[r].Cells[5].Value.ToString().Trim();
                worksheet.Cells[r + 2, 6] = yinhao + dataGridView1.Rows[r].Cells[6].Value.ToString().Trim();
                worksheet.Cells[r + 2, 7] = yinhao + dataGridView1.Rows[r].Cells[7].Value.ToString().Trim();
                worksheet.Cells[r + 2, 8] = yinhao + dataGridView1.Rows[r].Cells[8].Value.ToString().Trim();
                worksheet.Cells[r + 2, 9] = yinhao + dataGridView1.Rows[r].Cells[9].Value.ToString().Trim();
                worksheet.Cells[r + 2, 10] = dataGridView1.Rows[r].Cells[10].Value;

                //库存<=3,导出后在Excel中也变红
                for (int c = 1; c < dataGridView1.Columns.Count; c++)
                {
                    if (Convert.ToInt32(dataGridView1.Rows[r].Cells[9].Value) <= 3)
                    {
                        worksheet.Rows[r + 2].Interior.Color = ColorTranslator.ToOle(Color.Red);
                    }
                }

                System.Windows.Forms.Application.DoEvents();

            }

            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

            MessageBox.Show(fileName + "导出成功", "提示", MessageBoxButtons.OK);

            if (saveFileName != "")
            {

                try
                {
                    workbook.Saved = true;

                    workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 

                }

                catch (Exception ex)
                {//fileSaved = false;                      

                    MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);

                }

            }

            xlApp.Quit();

            GC.Collect();//强行销毁           }
        }

2.完成。

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值