NPOI怎么读取EXCEL的多个sheet,不使用com组件
展开全部
借用NPOI来实现,要在同一Excel文件中创建多个sheet,只需要在同一个workbook中创建多个sheet即可。要注意的是,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;
}
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;
}
展开全部
framework 2.0 版本的用法
using (FileStream sr = new FileStream("test.xls", FileMode.OpenOrCreate))
{
//根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档
HSSFWorkbook workbook = new HSSFWorkbook(sr);
int x = workbook.Workbook.NumSheets;
List<string> sheetNames = new List<string>();
for (int i = 0; i < x; i++)
{
sheetNames.Add(workbook.Workbook.GetSheetName(i));
}
using (FileStream sr = new FileStream("test.xls", FileMode.OpenOrCreate))
{
//根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档
HSSFWorkbook workbook = new HSSFWorkbook(sr);
int x = workbook.Workbook.NumSheets;
List<string> sheetNames = new List<string>();
for (int i = 0; i < x; i++)
{
sheetNames.Add(workbook.Workbook.GetSheetName(i));
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询