C#怎么导出dataGridView中的值到Excel

如题所述

  /// <summary>
        /// 将DataGridView数据转化为DataTable,不包括隐藏列
        /// </summary>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public DataTable DgvToDataTable(DataGridView dgv)
        {
            List<DataColumn > colList = new List< DataColumn>();
            DataTable dt = new DataTable();
            for (int count = 0; count < dgv.Columns.Count; count++)
            {
                DataColumn dc = new DataColumn(dgv.Columns[count].HeaderText);
                dt.Columns.Add(dc);

                if (dgv.Columns[count].Visible == false )
                    colList.Add(dc);
            }
            for (int count = 0; count < dgv.Rows.Count; count++)
            {
                DataRow dr = dt.NewRow();
                for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
                {
                    if (dgv.Columns[countsub].Visible == false)
                        continue;
                    dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
                }
                dt.Rows.Add(dr);
            }

            foreach (var col in colList)
            {
                dt.Columns.Remove(col);
            }
           
            return dt;
        }

/// <summary>
        /// 导出DataTable到Excel文件,使用Aspose.Cells.dll
        /// </summary>
        /// <param name="dt"> 要导出的数据 </param>
        /// <param name="path"> 保存路径</param>
        public static void ExportDtToExcel( DataTable dt, string path)
        {
            Workbook workbook = new Workbook(); //工作簿
            Worksheet sheet = workbook.Worksheets[0]; //工作表
            Cells cells = sheet.Cells;//单元格

            //样式2
            Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
            //style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
            style2.Font.Name = "宋体";//文字字体
            style2.Font.Size = 9; //文字大小
            style2.Font.IsBold = true;//粗体
            style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

            //样式3
            Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式
            //style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中
            style3.Font.Name = "宋体";//文字字体
            style3.Font.Size = 9; //文字大小
            style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

            int Colnum = dt.Columns.Count;//表格列数
            int Rownum = dt.Rows.Count;//表格行数

            //生成行1 列名行
            for (int i = 0; i < Colnum; i++)
            {
                cells[0, i].PutValue(dt.Columns[i].ColumnName);
                cells[0, i].SetStyle(style2);
                //cells.SetRowHeight(0, 20);
            }

            //生成数据行
            for (int i = 0; i < Rownum; i++)
            {
                for (int k = 0; k < Colnum; k++)
                {
                    cells[1 + i, k].PutValue(dt.Rows[i][k].ToString());
                    cells[1 + i, k].SetStyle(style3);
                }
                //cells.SetRowHeight(1 + i, 20);
            }

            workbook.Save(path);
        }

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-11-22
循环读取dataGridView中的行,写到excel文件中。
第2个回答  2016-11-22
aspose.cell,自己百度下,网上全是实例
相似回答