zoukankan      html  css  js  c++  java
  • datagridView数据导出到Excel

     try
                {
                     Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
               excel.Application.Workbooks.Add(true);

               
                    if (ds.Tables.Contains("viewPigSlice") && ds.Tables["viewPigSlice"].Rows.Count > 0)
                    {
                        //生成字段
                        for(int i=0;i<dgvViewPigSliceInfo.Columns.Count;i++)
                        {
                            excel.Cells[1, i + 1] = dgvViewPigSliceInfo.Columns[i].HeaderCell.Value;
                        }
                        //填充数据
                        for (int i = 0; i < dgvViewPigSliceInfo.Rows.Count; i++)
                        {
                            for(int j=0;j<dgvViewPigSliceInfo.Columns.Count;j++)
                            {
                                if(dgvViewPigSliceInfo.Rows[i].Cells[j].Value==typeof(string))
                                {
                                    excel.Cells[i + 2,j + 1] =""+ dgvViewPigSliceInfo.Rows[i].Cells[j].Value.ToString();

                                }else
                                {
                                    excel.Cells[i + 2, j + 1] = dgvViewPigSliceInfo.Rows[i].Cells[j].Value.ToString();
     
                                }
                            }
                        }
                      

                        excel.Visible = true;

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

  • 相关阅读:
    python-pycharm-django
    CSS
    django邮件
    访问user Model的三种方式
    weblogic升级war包(工作备忘)
    RestfulAPI_ 验证 与授权
    Restful API serialize相关
    scripy login captcha
    linux环境设置和核心命令
    java 调用JIRA api接口
  • 原文地址:https://www.cnblogs.com/kingwangzhen/p/1459658.html
Copyright © 2011-2022 走看看