ug組件在被排除的引用集,NPOI組件下載、引用、基本使用

 2023-10-24 阅读 26 评论 0

摘要:前言 NPOI是一組dll庫文件,也叫COM組件。對于Excel表格用代碼操作,主要有三種方式: 1.OLEDB類似于數據庫的操作,需要安裝AccessDataBase數據庫引擎; 2.Office組件,也是引用dll文件,依賴Office軟件,并且會因Office的版
  • 前言

NPOI是一組dll庫文件,也叫COM組件。對于Excel表格用代碼操作,主要有三種方式:

1.OLEDB類似于數據庫的操作,需要安裝AccessDataBase數據庫引擎;
2.Office組件,也是引用dll文件,依賴Office軟件,并且會因Office的版本有一些區別和問題;
3.NPOI組件,就是在程序中添加dll文件,然后進行Excel表格的讀寫,表格處理。

  • 背景

我是用NPOI+C#的模式完成了表格的處理,并且不依賴任何的辦公軟件,比Office組件好用。處理的語句與Office的語句模式大致相同,細節略有不同。

  • NPOI組件dll文件的來源

ug組件在被排除的引用集?官網下載:http://npoi.codeplex.com/releases/

在這里插入圖片描述
----解壓,得到Release文件夾,其中有net20和net40,選擇版本較高的net40。

----項目引用,右鍵添加引用,加入到項目中。
在這里插入圖片描述

  • 使用

注意:

未能找到引用組件adodb?NPOI 使用 HSSFWorkbook 類來處理 xls,XSSFWorkbook 類來處理 xlsx,它們都繼承接口 IWorkbook,因此可以通過 IWorkbook 來統一處理 xls 和 xlsx 格式的文件。

添加頭文件:

using NPOI;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;

界面加一個button1,然后添加如下代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;using NPOI;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;namespace NPOI_Test1
{
public partial class Page1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}protected void btnExport_Click(object sender, EventArgs e){SqlConnection cn = new SqlConnection();cn.ConnectionString = "server=.;uid=sa;pwd=密碼不告訴你;database=dawufan";cn.Open();string sqlstr = @"select * from interest";SqlCommand cmd = new SqlCommand();cmd.Connection = cn;cmd.CommandText = sqlstr;SqlDataReader reader = cmd.ExecuteReader();DataTable dt = ReaderToTable(reader);ExportExcel(dt);cn.Close();cn.Dispose();cmd.Dispose();reader.Close();dt.Dispose();}protected DataTable ReaderToTable(SqlDataReader dr){DataTable dt = new DataTable();for (int i = 0; i < dr.FieldCount; i++){dt.Columns.Add(dr.GetName(i), dr.GetFieldType(i));}object[] objValues = new object[dr.FieldCount];while (dr.Read()){dr.GetValues(objValues);dt.LoadDataRow(objValues, true);}dr.Close();return dt;}protected void ExportExcel(DataTable dt){HttpContext curContext = HttpContext.Current;//設置編碼及附件格式curContext.Response.ContentType = "application/vnd.ms-excel";curContext.Response.ContentEncoding = Encoding.UTF8;curContext.Response.Charset = "";string fullName = HttpUtility.UrlEncode("FileName.xls", Encoding.UTF8);curContext.Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(fullName, Encoding.UTF8));  //attachment后面是分號byte[] data = TableToExcel(dt, fullName).GetBuffer();curContext.Response.BinaryWrite(TableToExcel(dt, fullName).GetBuffer());curContext.Response.End();}public MemoryStream TableToExcel(DataTable dt, string file){//創建workbookIWorkbook workbook;string fileExt = Path.GetExtension(file).ToLower();if (fileExt == ".xlsx")workbook = new XSSFWorkbook();else if (fileExt == ".xls")workbook = new HSSFWorkbook();elseworkbook = null;//創建sheetISheet sheet = workbook.CreateSheet("Sheet1");//表頭IRow headrow = sheet.CreateRow(0);for (int i = 0; i < dt.Columns.Count; i++){ICell headcell = headrow.CreateCell(i);headcell.SetCellValue(dt.Columns[i].ColumnName);}//表內數據for (int i = 0; i < dt.Rows.Count; i++){IRow row = sheet.CreateRow(i + 1);for (int j = 0; j < dt.Columns.Count; j++){ICell cell = row.CreateCell(j);cell.SetCellValue(dt.Rows[i][j].ToString());}}//轉化為字節數組MemoryStream ms = new MemoryStream();workbook.Write(ms);ms.Flush();ms.Position = 0;return ms;}}}
  • 設置樣式

Step1. 表頭單元格樣式
表頭樣式設置水平居中、大小14、黃背景紅字、紅底線。參考代碼如下:

//表頭樣式
ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = HorizontalAlignment.Center;
IFont font = workbook.CreateFont();
font.Boldweight = 20;
font.FontHeightInPoints = 14;
font.Color = HSSFColor.Red.Index;
headStyle.SetFont(font);
//以下三行為背景色
headStyle.FillForegroundColor = HSSFColor.Yellow.Index;
headStyle.FillPattern = FillPattern.Squares;
headStyle.FillBackgroundColor = HSSFColor.Yellow.Index;
//border
headStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium;
headStyle.BottomBorderColor = HSSFColor.Red.Index;

組件調用另一個組件的方法,Step2. 普通單元格樣式
普通單元格樣式設置水平居中、垂直居中、綠字。參考代碼如下:

//普通單元格樣式
ICellStyle bodyStyle = workbook.CreateCellStyle();
bodyStyle.Alignment = HorizontalAlignment.Center;
bodyStyle.VerticalAlignment = VerticalAlignment.Center;
IFont font1 = workbook.CreateFont();
font1.Color = HSSFColor.Green.Index;
font1.Boldweight = 20;
bodyStyle.SetFont(font1);

Step3. 合并行
合并行使用 ISheet.AddMergedRegion 方法來實現。對于本文示例,可以從第0列即id列入手,在對sheet的行進行遍歷的基礎上,對于具有相同id的行的前兩列進行縱向合并。

參考代碼如下:

//合并行
for(int i = 1; i < dt.Rows.Count + 1; i++)
{string value = sheet.GetRow(i).GetCell(0).StringCellValue;int end = i;//找到結束為止for(int j = i + 1; j < dt.Rows.Count + 1; j++){string value1= sheet.GetRow(j).GetCell(0).StringCellValue;if (value != value1){end = j - 1;break;}else if(value==value1 && j == dt.Rows.Count){end = j;break;}}sheet.AddMergedRegion(new CellRangeAddress(i, end, 0, 0));sheet.AddMergedRegion(new CellRangeAddress(i, end, 1, 1));i = end;
}

Step4. 設置列寬
參考代碼如下:

//列寬
sheet.SetColumnWidth(0, 20 * 256);
sheet.SetColumnWidth(1, 20 * 256);
sheet.SetColumnWidth(2, 20 * 256);
sheet.SetColumnWidth(3, 20 * 256);
  • 結束語

pd.pivot_table、本文介紹了在C#項目中使用NPOI組件進行表格處理的方法和步驟,以及DLL文件的來源,和實際使用的代碼示例,記錄在這里方便大家,還有我自己參考查閱。

參考:https://blog.csdn.net/wf824284257/article/details/77113691

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/165357.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息