C# DataTable导出Excel

[亡魂溺海] 提交于 2020-04-01 06:04:45
 /// <summary>
        /// DataTable导出到Excel
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="array">首行数组</param>
        /// <param name="fileFullName">文件名</param>
        public static void ExportToExcel(DataTable dt, string[] array, string fileFullName)
        {
            #region
            XSSFWorkbook workbook = new XSSFWorkbook();
            FileStream fs = new FileStream(fileFullName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            ISheet sheet = workbook.CreateSheet("Sheet0");
            IRow row;
            row = sheet.CreateRow(0);
            
            for (int i = 0; i < array.Length; i++)
            {
                row.CreateCell(i).SetCellValue(array[i]);
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                row = sheet.CreateRow(i + 1);

                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    row.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
                }
            }
            workbook.Write(fs); //写入到excel

            #endregion
            // MessageBox.Show("导出成功!");
        }

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!