NPOI2.1.1生成Excel文件(C#)
几经周折,总结如下:
首先,下载NPOI2.1.1。
之后解压,VS项目中右击引用,然后选中所有dll文件引入到项目中即可。
再之后创建xls文件,这样做:
public void create()
{
HSSFWorkbook hssfworkbook
= new HSSFWorkbook();
ISheet sheet
= hssfworkbook
.CreateSheet("Sheet1");
hssfworkbook
.CreateSheet("Sheet2");
hssfworkbook
.CreateSheet("Sheet3");
IRow row
= sheet
.CreateRow(0);
ICell cell
= row
.CreateCell(0);
cell
.SetCellValue("ddddd");
ICellStyle style
= hssfworkbook
.CreateCellStyle();
style
.Alignment
= NPOI
.SS
.UserModel
.HorizontalAlignment
.Center
;
IFont font
= hssfworkbook
.CreateFont();
font
.FontHeight
= 20 * 20;
style
.SetFont(font
);
cell
.CellStyle
= style
;
sheet
.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 4));
string filePath
= @"C:\Users\Administrator\Desktop\Hello.xls";
FileStream file
= new FileStream(filePath
, FileMode
.Create
);
hssfworkbook
.Write(file
);
file
.Close();
}
运行程序即可看到桌面上有了Hello.xls文件。
NPOI导Excel参考
using NPOI
.HSSF
.UserModel
;
using NPOI
.SS
.UserModel
;
using NPOI
.HSSF
.Util
;
using System
.IO
;
using System
.Collections
.Generic
;
using System
;
using System
.Data
;
namespace Project202
{
class DataOut
{
public DataOut()
{
}
public int Save(DataTable dt
, string[] headLine
)
{
FileStream fileExcel
;
HSSFWorkbook workBook
= new HSSFWorkbook();
HSSFSheet Sheet1
;
int largestNum
= 0;
string fileName
;
if(dt
.TableName
=="Data")
fileName
= DateTime
.Now
.ToString("导出数据(yyyyMMdd-HH时mm分ss秒)");
else
fileName
= DateTime
.Now
.ToString("导出日志(yyyyMMdd-HH时mm分ss秒)");
if (Directory
.Exists(Setting
.savePath
) == false)
Directory
.CreateDirectory(Setting
.savePath
);
else
{
DirectoryInfo di
= new DirectoryInfo(Setting
.savePath
);
foreach (FileInfo file
in di
.GetFiles("*.xls"))
{
if (file
.Name
.StartsWith(fileName
))
{
int tempNum
;
try
{
tempNum
= int.Parse(file
.Name
.Substring(7, (int)file
.Name
.Length
- 11));
largestNum
= tempNum
> largestNum
? tempNum
: largestNum
;
}
catch
{
largestNum
= 1;
}
}
}
}
fileName
= Setting
.savePath
+ fileName
;
if (++largestNum
> 1)
fileName
+= "-" + (largestNum
- 1).ToString() + ".xls";
else
fileName
+= ".xls";
if (workBook
.GetSheet("数据") == null)
{
Sheet1
= (HSSFSheet
)workBook
.CreateSheet("数据");
}
else
Sheet1
= (HSSFSheet
)workBook
.GetSheet("数据");
Sheet1
.CreateRow(Sheet1
.LastRowNum
);
Sheet1
.SetColumnWidth(0, 24 * 256);
Sheet1
.SetColumnWidth(1, 13 * 256);
Sheet1
.SetColumnWidth(2, 13 * 256);
HSSFRow sheetRow
= (HSSFRow
)Sheet1
.GetRow(Sheet1
.LastRowNum
);
HSSFCell
[] sheetCell
= new HSSFCell[dt
.Columns
.Count
];
if (Sheet1
.LastRowNum
== 0)
{
HSSFCellStyle headStyle
= workBook
.CreateCellStyle() as HSSFCellStyle
;
headStyle
.Alignment
= NPOI
.SS
.UserModel
.HorizontalAlignment
.Center
;
HSSFFont headFont
= workBook
.CreateFont() as HSSFFont
;
headFont
.FontHeightInPoints
= 10;
headFont
.Boldweight
= 700;
headStyle
.SetFont(headFont
);
HSSFCell
[] headCell
= new HSSFCell[headLine
.Length
];
for (int j
= 0; j
< headLine
.Length
; j
++)
{
headCell
[j
] = (HSSFCell
)sheetRow
.CreateCell(j
);
headCell
[j
].SetCellValue(headLine
[j
]);
headCell
[j
].CellStyle
= headStyle
;
}
Sheet1
.CreateRow(Sheet1
.LastRowNum
+ 1);
sheetRow
= (HSSFRow
)Sheet1
.GetRow(Sheet1
.LastRowNum
);
}
ICellStyle cellStyle
= workBook
.CreateCellStyle();
cellStyle
.Alignment
= HorizontalAlignment
.Center
;
ICellStyle cellStyleText
= workBook
.CreateCellStyle();
cellStyleText
.Alignment
= HorizontalAlignment
.Center
;
cellStyleText
.DataFormat
= HSSFDataFormat
.GetBuiltinFormat("text");
HSSFCellStyle rowStyle
= workBook
.CreateCellStyle() as HSSFCellStyle
;
Sheet1
.SetDefaultColumnStyle(3, cellStyle
);
Sheet1
.SetDefaultColumnStyle(0, cellStyleText
);
Sheet1
.SetDefaultColumnStyle(1, cellStyleText
);
Sheet1
.SetDefaultColumnStyle(2, cellStyleText
);
ICellStyle tempStyle
= workBook
.CreateCellStyle();
tempStyle
.CloneStyleFrom(cellStyleText
);
IFont tempFont
= workBook
.CreateFont();
tempFont
.Color
= HSSFColor
.Red
.Index
;
tempStyle
.SetFont(tempFont
);
for (int i
= 0;i
<dt
.Rows
.Count
;++i
)
{
for (int j
= 0; j
< headLine
.Length
; j
++)
{
sheetCell
[j
] = (HSSFCell
)sheetRow
.CreateCell(j
);
string strValue
= dt
.Rows
[i
][j
].ToString();
switch (dt
.Columns
[j
].DataType
.ToString())
{
case "System.Int16":
case "System.Int32":
case "System.Int64":
int intV
= 0;
int.TryParse(strValue
, out intV
);
sheetCell
[j
].SetCellValue(intV
);
break;
default:
sheetCell
[j
].SetCellValue(strValue
);
if (strValue
== "True")
sheetCell
[j
].CellStyle
= tempStyle
;
break;
}
}
if (Sheet1
.LastRowNum
< 65535)
Sheet1
.CreateRow(Sheet1
.LastRowNum
+ 1);
sheetRow
= (HSSFRow
)Sheet1
.GetRow(Sheet1
.LastRowNum
);
}
lock (this)
{
fileExcel
= new FileStream(fileName
, FileMode
.Create
, FileAccess
.ReadWrite
);
workBook
.Write(fileExcel
);
fileExcel
.Close();
return Sheet1
.LastRowNum
-1;
}
}
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-5429.html