npoi导出execl源码,vs2008实现,包括using库
該鏈接有導入,導出源碼,我的代碼有下鏈接改寫,完善而成的,
http://www.cnblogs.com/colder/p/3611906.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Data;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.POIFS;
using NPOI.Util;
using NPOI.HSSF.Util;
using NPOI.HSSF.Extractor;
using NPOI.SS.UserModel;
using System.Web.UI.HtmlControls;
private void to_excel()
{
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Sheet1");
MemoryStream ms = new MemoryStream();
ICellStyle HeadercellStyle = workbook.CreateCellStyle();
HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
HeadercellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
//字體
NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
headerfont.Boldweight = (short)FontBoldWeight.Bold;
HeadercellStyle.SetFont(headerfont);
//用column name 作為列名
string[] temArr = { "帳號", "子帳號", "客戶經理號(8位)", "開戶日期(YYYYMMDD)" };
List<string> headlist = new List<string>(temArr);
int icolIndex = 0;
IRow headerRow = sheet.CreateRow(0);
foreach (var item in headlist)
{
ICell cell = headerRow.CreateCell(icolIndex);
cell.SetCellValue(item);
cell.CellStyle = HeadercellStyle;
icolIndex++;
}
ICellStyle cellStyle = workbook.CreateCellStyle();
//為避免日期格式被Excel自動替換,所以設定 format 為 『@』 表示一率當成text來看
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
cellfont.Boldweight = (short)FontBoldWeight.Normal;
cellStyle.SetFont(cellfont);
//建立內容行
TMSDataContext dc = new TMSDataContext();
var lctlist = dc.wd_lct_dq_import.ToList();
int iRowIndex = 1;
//int iCellIndex = 0;
foreach (var lct in lctlist)
{
IRow DataRow = sheet.CreateRow(iRowIndex);
//第1列 賬號
ICell cell = DataRow.CreateCell(0);
cell.SetCellValue(lct.zh);
cell.CellStyle = cellStyle;
//第2列 子賬號
cell = DataRow.CreateCell(1);
cell.SetCellValue(lct.zh_sub);
cell.CellStyle = cellStyle;
//第3列 客戶經理號
cell = DataRow.CreateCell(2);
cell.SetCellValue(lct.emp_lch);
cell.CellStyle = cellStyle;
//第4列 開戶日期
cell = DataRow.CreateCell(3);
cell.SetCellValue(lct.kh_rq);
cell.CellStyle = cellStyle;
iRowIndex++;
}
//自適應列寬度
for (int i = 0; i < icolIndex; i++)
{
sheet.AutoSizeColumn(i);
}
workbook.Write(ms);
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=EmptyWorkbook.xls"));
Response.BinaryWrite(ms.ToArray());
workbook = null;
ms.Close();
ms.Dispose();
}
轉載于:https://www.cnblogs.com/shanwater/p/5638020.html
總結
以上是生活随笔為你收集整理的npoi导出execl源码,vs2008实现,包括using库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个炫酷的个性签名。
- 下一篇: 客车上大吧车上的电影都是哪来的为啥硬盘能