NPOI는 여러 dt 데이터를 EXCEL의 다중 시트로 가져옵니다.

6224 단어 Excel
    /// <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();

        }
    }

좋은 웹페이지 즐겨찾기