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();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Excel Grep toolExcel Grep tool ■히나가타 ■ 시트 구성 ExcelGrep.cls...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.