上一篇介绍了Java操作execl用的是POI,这一篇介绍c#操作execl,采用NPOI,
所需引用:NPOI
NPOI.OOXML
NPOI.OpenXml4Net
NPOI.OpenXmlFormats
下载地址:http://download.csdn.net/download/qq_37791764/10173729
代码部分:
//读取excel文件的路径
string filePath = @"" + Server.MapPath("~/") + "PBTable\\execl\\排班表.xls";
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
//创建workbook
HSSFWorkbook wk = new HSSFWorkbook(fs);
//在第一个sheet写入操作
NPOI.SS.UserModel.Sheet sheet = (HSSFSheet)wk.GetSheetAt(0);
//创建
Row row0 = sheet.CreateRow(0);
Row row1 = sheet.CreateRow(1);
Row row2 = sheet.CreateRow(2);
Row row3 = sheet.CreateRow(3);
Row row4 = sheet.CreateRow(4);
HSSFPalette Salepalette = wk.GetCustomPalette();
CellStyle SalecolorStyle = wk.CreateCellStyle();
//设置边框格式
SalecolorStyle.BorderBottom = NPOI.SS.UserModel.CellBorderType.THIN;
SalecolorStyle.BorderLeft = NPOI.SS.UserModel.CellBorderType.THIN;
SalecolorStyle.BorderRight = NPOI.SS.UserModel.CellBorderType.THIN;
SalecolorStyle.BorderTop = NPOI.SS.UserModel.CellBorderType.THIN;
//垂直对齐
SalecolorStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER;
//水平对齐
SalecolorStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
for (int i = 0; i < 35; i++)
{
Cell cell0 = row0.CreateCell(i);
Cell cell1 = row1.CreateCell(i);
Cell cell2 = row2.CreateCell(i);
Cell cell3 = row3.CreateCell(i);
Cell cell4 = row4.CreateCell(i);
if(i==2){
row1.GetCell(i).SetCellValue("/日期");
row2.GetCell(i).SetCellValue("节日/活动");
row3.GetCell(i).SetCellValue("营业展开计划");
row4.GetCell(i).SetCellValue("事项");
}
if(i==(34)){
row0.GetCell(i).SetCellValue("工时");
}
if (i >= 3 && i < (MonthDayCount+