您的位置:首页 > 编程语言 > ASP

ASP.NET将数据导出到一个EXCEL文件的多个SHEET中

2016-06-05 22:42 591 查看
一、实现原理

 

使用用NPOI实现在同一Excel文件中创建多个sheet,只需要在同一个Excel中创建多个sheet即可。

 

 

二、实例

 

private void buttonTest_Click(object sender, EventArgs e)
{
HSSFWorkbook workBook = new HSSFWorkbook();
//ISheet sheetA = workBook.CreateSheet("sheetA");
//ISheet sheetB = workBook.CreateSheet("sheetB");

createSheet(workBook,"SheetA");
createSheet(workBook,"SheetB");
createSheet(workBook,"SheetC");

string path = Application.StartupPath + @"\\test.xls";
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream file = new FileStream(path, FileMode.Create))
{
workBook.Write(file);  //创建Excel文件。
file.Close();
}
MessageBox.Show("OK");
}

private ISheet createSheet(HSSFWorkbook workBook, string sheetName)
{
ISheet sheet = workBook.CreateSheet(sheetName);
IRow RowHead = sheet.CreateRow(0);

for (int iColumnIndex = 0; iColumnIndex < 10; iColumnIndex++)
{
RowHead.CreateCell(iColumnIndex).SetCellValue(Guid.NewGuid().ToString());
}

for (int iRowIndex = 0; iRowIndex < 20; iRowIndex++)
{
IRow RowBody = sheet.CreateRow(iRowIndex + 1);
for (int iColumnIndex = 0; iColumnIndex < 10; iColumnIndex++)
{
RowBody.CreateCell(iColumnIndex).SetCellValue(DateTime.Now.Millisecond);
sheet.AutoSizeColumn(iColumnIndex);
}
}
return sheet;
}


 

文章转载自:ASP.NET将数据导出到一个EXCEL文件的多个SHEET中  http://www.studyofnet.com/news/1264.html
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ASP.NET