还记得小学时候学的九九乘法表吗?这节我们一起学习利用NPOI通过C#代码生成一张Excel的九九乘法表。要生成九九乘法表,循环肯定是少不了的,如下:
代码其实很简单,就是循环调用cell.SetCellValue(str)写入9行数据,每一行写的单元格数量随行数递增。执行完后生成的Excel样式如下:

完整的代码如下:
TimesTables
{
public class Program
{
static HSSFWorkbook hssfworkbook;
static void Main(string[] args)
{
InitializeWorkbook();
HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
HSSFRow row;
HSSFCell cell;
for (int rowIndex = 0; rowIndex < 9; rowIndex++)
{
row = sheet1.CreateRow(rowIndex);
for (int colIndex = 0; colIndex <= rowIndex; colIndex++)
{
cell = row.CreateCell(colIndex);
cell.SetCellValue(String.Format("{0}*{1}={2}", rowIndex + 1, colIndex + 1, (rowIndex + 1) * (colIndex + 1)));
}
}
WriteToFile();
}
static void WriteToFile()
{
//Write the stream data of workbook to the root directory
FileStream file = new FileStream(@"test.xls", FileMode.Create);
hssfworkbook.Write(file);
file.Close();
}
static void InitializeWorkbook()
{
hssfworkbook = new HSSFWorkbook();
//create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI Team";
hssfworkbook.DocumentSummaryInformation = dsi;
//create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "NPOI SDK Example";
hssfworkbook.SummaryInformation = si;
}
}
}
HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
HSSFRow row;
HSSFCell cell;
for (int rowIndex = 0; rowIndex < 9; rowIndex++)
{
row = sheet1.CreateRow(rowIndex);
for (int colIndex = 0; colIndex <= rowIndex; colIndex++)
{
cell = row.CreateCell(colIndex);
cell.SetCellValue(String.Format("{0}*{1}={2}", rowIndex + 1, colIndex + 1, (rowIndex + 1) * (colIndex + 1)));
}
}
HSSFRow row;
HSSFCell cell;
for (int rowIndex = 0; rowIndex < 9; rowIndex++)
{
row = sheet1.CreateRow(rowIndex);
for (int colIndex = 0; colIndex <= rowIndex; colIndex++)
{
cell = row.CreateCell(colIndex);
cell.SetCellValue(String.Format("{0}*{1}={2}", rowIndex + 1, colIndex + 1, (rowIndex + 1) * (colIndex + 1)));
}
}
代码其实很简单,就是循环调用cell.SetCellValue(str)写入9行数据,每一行写的单元格数量随行数递增。执行完后生成的Excel样式如下:
完整的代码如下:
{
public class Program
{
static HSSFWorkbook hssfworkbook;
static void Main(string[] args)
{
InitializeWorkbook();
HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
HSSFRow row;
HSSFCell cell;
for (int rowIndex = 0; rowIndex < 9; rowIndex++)
{
row = sheet1.CreateRow(rowIndex);
for (int colIndex = 0; colIndex <= rowIndex; colIndex++)
{
cell = row.CreateCell(colIndex);
cell.SetCellValue(String.Format("{0}*{1}={2}", rowIndex + 1, colIndex + 1, (rowIndex + 1) * (colIndex + 1)));
}
}
WriteToFile();
}
static void WriteToFile()
{
//Write the stream data of workbook to the root directory
FileStream file = new FileStream(@"test.xls", FileMode.Create);
hssfworkbook.Write(file);
file.Close();
}
static void InitializeWorkbook()
{
hssfworkbook = new HSSFWorkbook();
//create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI Team";
hssfworkbook.DocumentSummaryInformation = dsi;
//create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "NPOI SDK Example";
hssfworkbook.SummaryInformation = si;
}
}
}