日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

npoi导出execl源码,vs2008实现,包括using库

發布時間:2023/11/27 生活经验 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。