DataGridView 导出Excel

来源:互联网 发布:c语言 打印字节数组 编辑:程序博客网 时间:2024/05/21 09:14

    #region 测试Excel能否打开
            Excel.Application excel = new Excel.Application();//引用Excel对象
            excel.Application.Workbooks.Add(true);//引用Excel工作簿
            excel.Visible = true;//使Excel可视
            excel = null; //释放资源          
            #endregion

 

 

 

public bool ExportDataTable(DataGridView dgv, int Columns)
        {
            //建立Excel对象
            Excel.Application excel = new Excel.Application();
            excel.Application.Workbooks.Add(true);
            excel.Visible = true;

            object[,] objArray = null;
            objArray = new object[dgv.Rows.Count, dgv.Columns.Count];

       
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                for (int j = 0; j < dgv.Columns.Count-1; j++)
                {
                    objArray[i, j] = dgv.Rows[i].Cells[j+1].Value.ToString();
                }
            }

            //生成字段名称
            excel.get_Range(excel.Cells[1, 1], excel.Cells[1, Columns]).Merge(0);//合并第一行1-14单元格
            excel.Cells[1, 1] = " ";
            excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).HorizontalAlignment = XlHAlign.xlHAlignCenter;//居中显示
            excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Name = "黑体";//字体
            excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Size = 14;
            excel.get_Range(excel.Cells[2, 1], excel.Cells[2, Columns]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            excel.get_Range(excel.Cells[2, 1], excel.Cells[2, Columns]).Font.Size = 12;
            excel.get_Range(excel.Cells[2, 1], excel.Cells[2, Columns]).Borders.LineStyle = 1;
            excel.get_Range(excel.Cells[2, 1], excel.Cells[2, Columns]).Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            excel.get_Range(excel.Cells[2, 1], excel.Cells[2, Columns]).Cells.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
            excel.get_Range(excel.Cells[3, 1], excel.Cells[dgv.Rows.Count + 2, dgv.Columns.Count]).Value2 = objArray;

            excel.get_Range(excel.Cells[2, 1], excel.Cells[2, 1]).ColumnWidth = 16;
            excel.Cells[2, 1] = "备件名称";
            excel.get_Range(excel.Cells[2, 2], excel.Cells[2, 2]).ColumnWidth = 14;
            excel.Cells[2, 2] = "备件型号";
            excel.get_Range(excel.Cells[2, 3], excel.Cells[2, 3]).ColumnWidth = 10;
            excel.Cells[2, 3] = "备件条码";
            excel.get_Range(excel.Cells[2, 4], excel.Cells[2, 4]).ColumnWidth = 15;
            excel.Cells[2, 4] = "序列号";
            excel.get_Range(excel.Cells[2, 5], excel.Cells[2, 5]).ColumnWidth = 15;
            excel.Cells[2, 5] = "备注";
            excel.Cells[2, 6] = "备件状态";
            excel.get_Range(excel.Cells[2, 7], excel.Cells[2, 7]).ColumnWidth = 15;
            excel.Cells[2, 7] = "导致状态原因";
            excel.get_Range(excel.Cells[2, 8], excel.Cells[2, 8]).ColumnWidth = 10;
            excel.Cells[2, 8] = "存放位置";
            excel.Cells[2, 9] = "大箱号";
            excel.Cells[2, 10] = "小箱号";
            excel.Cells[2, 11] = "厂家";
            excel.get_Range(excel.Cells[2, 12], excel.Cells[2, 12]).ColumnWidth = 10;
            excel.Cells[2, 12] = "设备类型";
            excel.get_Range(excel.Cells[2, 13], excel.Cells[2, 13]).ColumnWidth = 10;                
            excel.Cells[2, 13] = "光传输容量";
            excel.get_Range(excel.Cells[2, 14], excel.Cells[2, 14]).ColumnWidth = 10;
            excel.Cells[2, 14] = "接口类型";
            excel.get_Range(excel.Cells[2, 15], excel.Cells[2, 15]).ColumnWidth = 10;                
            excel.Cells[2, 15] = "光口参数";
            excel.get_Range(excel.Cells[2, 16], excel.Cells[2, 16]).ColumnWidth = 10;
            excel.Cells[2, 16] = "来源项目";
            excel.get_Range(excel.Cells[2, 17], excel.Cells[2, 17]).ColumnWidth = 16;
            excel.Cells[2, 17] = "最近变更时间";                      

            Excel.Sheets sheet = excel.Sheets;
            return true;
        }

 

原创粉丝点击