NPOI实现多个dt数据导入一个EXCEL的多SHEET

    /// <summary>
        /// 导出EXCEL
        /// </summary>
        /// <param name="sTableNames">所有表名</param>
        /// <param name="strFileName">导出文件名</param>
        public static void ExportEasy(string[] sTableNames,string strFileName,string sStartDate,string sEndDate,string sStartHour,string sEndHour)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            for (int iTCount = 0; iTCount < sTableNames.Length; iTCount++)
            {
                DBTables dbs = new DBTables();
                string[] sFieldName=new string[3];
                List<string> lstFieldName = new List<string>();
                lstFieldName = dbs.GetColumnFieldList(sTableNames[iTCount]);
                string sSql="";
                if (lstFieldName.Contains("结束时间"))
                {
                    sSql = "SELECT * FROM [" + sTableNames[iTCount] + "] WHERE 日期>='" + sStartDate + "' AND 日期<='" + sEndDate + "' AND 结束时间>=" + sStartHour + "AND 结束时间<=" + sEndDate;
                }
                else
                {
                    sSql = "SELECT * FROM [" + sTableNames[iTCount] + "] WHERE 日期>='" + sStartDate + "' AND 日期<='" + sEndDate + "' AND 时间>=" + sStartHour + "AND 时间<=" + sEndDate;
                }
                DataSet ds = new DataSet();
                string[] s = { sTableNames[iTCount] };

                SqlHelper.FillDataset(Common.GetConnect(), CommandType.Text, sSql, ds, s);
                DataTable dt = ds.Tables[sTableNames[iTCount]];
                HSSFSheet sheet = workbook.CreateSheet(sTableNames[iTCount]);

                //填充表头
                HSSFRow dataRow = sheet.CreateRow(0);
                foreach (DataColumn column in dt.Columns)
                {
                    dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
                }

                //填充内容
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dataRow = sheet.CreateRow(i + 1);
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        dataRow.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
                    }
                }

            }

            //保存
            using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(fs);
                }
            }

        }
        /// <summary>
        /// 导出EXCEL,可以导出多个sheet
        /// </summary>
        /// <param name="dtSources">原始数据数组类型</param>
        /// <param name="strFileName">路径</param>
        public static void ExportEasy(DataTable[] dtSources, string strFileName)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            for (int k = 0; k < dtSources.Length; k++)
            {
                HSSFSheet sheet = workbook.CreateSheet(dtSources[k].TableName.ToString());

                //填充表头
                HSSFRow dataRow = sheet.CreateRow(0);
                foreach (DataColumn column in dtSources[k].Columns)
                {
                    dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
                }

                //填充内容
                for (int i = 0; i < dtSources[k].Rows.Count; i++)
                {
                    dataRow = sheet.CreateRow(i + 1);
                    for (int j = 0; j < dtSources[k].Columns.Count; j++)
                    {
                        dataRow.CreateCell(j).SetCellValue(dtSources[k].Rows[i][j].ToString());
                    }
                }
            }

            //保存
            using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(fs);
                }
            }
        }
        /// <summary>
        /// 导出单个EXCEL
        /// </summary>
        /// <param name="dtSource"></param>
        /// <param name="strFileName"></param>
        public static void ExportEasy(DataTable dtSource, string strFileName)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.CreateSheet();

            //填充表头
            HSSFRow dataRow = sheet.CreateRow(0);
            foreach (DataColumn column in dtSource.Columns)
            {
                dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
            }

            //填充内容
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                dataRow = sheet.CreateRow(i + 1);
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
                }
            }

            //保存
            using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(fs);
                }
            }
            //workbook.Dispose();

        }
    }
posted @ 2013-04-27 08:58  SOUTHER  阅读(604)  评论(0编辑  收藏  举报