欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#用NPOI導(dǎo)出導(dǎo)入Excel幫助類(lèi)

 更新時(shí)間:2022年02月21日 14:53:17   作者:lbx_15887055073  
這篇文章主要為大家詳細(xì)介紹了C#?NPOI導(dǎo)出導(dǎo)入Excel幫助類(lèi),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#用NPOI導(dǎo)出導(dǎo)入Excel幫助類(lèi)的具體代碼,供大家參考,具體內(nèi)容如下

1、準(zhǔn)備工作

在管理NuGet程序包中搜索 NPOI,安裝 NPOI 到要項(xiàng)目中。

2、幫助類(lèi)

/// <summary>
/// DataTable寫(xiě)入Excel
/// </summary>
/// <param name="dt"></param>
/// <param name="strExcelFileName"></param>
/// <returns></returns>
public bool GridToExcelByNPOI(DataTable dt, string strExcelFileName)
{
? ? try
? ? {
? ? ? ? HSSFWorkbook workbook = new HSSFWorkbook();
? ? ? ? ISheet sheet = workbook.CreateSheet("Sheet1");

? ? ? ? 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 作為列名
? ? ? ? int icolIndex = 0;
? ? ? ? IRow headerRow = sheet.CreateRow(0);
? ? ? ? foreach (DataColumn item in dt.Columns)
? ? ? ? {
? ? ? ? ? ? ICell cell = headerRow.CreateCell(icolIndex);
? ? ? ? ? ? cell.SetCellValue(item.ColumnName);
? ? ? ? ? ? cell.CellStyle = HeadercellStyle;
? ? ? ? ? ? icolIndex++;
? ? ? ? }

? ? ? ? ICellStyle cellStyle = workbook.CreateCellStyle();

? ? ? ? //為避免日期格式被Excel自動(dòng)替換,所以設(shè)定 format 為 『@』 表示一率當(dāng)成text來(lái)看
? ? ? ? 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);

? ? ? ? //建立內(nèi)容行
? ? ? ? int iRowIndex = 1;
? ? ? ? int iCellIndex = 0;
? ? ? ? foreach (DataRow Rowitem in dt.Rows)
? ? ? ? {
? ? ? ? ? ? IRow DataRow = sheet.CreateRow(iRowIndex);
? ? ? ? ? ? foreach (DataColumn Colitem in dt.Columns)
? ? ? ? ? ? {

? ? ? ? ? ? ? ? ICell cell = DataRow.CreateCell(iCellIndex);
? ? ? ? ? ? ? ? cell.SetCellValue(Rowitem[Colitem].ToString());
? ? ? ? ? ? ? ? cell.CellStyle = cellStyle;
? ? ? ? ? ? ? ? iCellIndex++;
? ? ? ? ? ? }
? ? ? ? ? ? iCellIndex = 0;
? ? ? ? ? ? iRowIndex++;
? ? ? ? }

? ? ? ? //自適應(yīng)列寬度
? ? ? ? for (int i = 0; i < icolIndex; i++)
? ? ? ? {
? ? ? ? ? ? sheet.AutoSizeColumn(i);
? ? ? ? }

? ? ? ? //寫(xiě)Excel
? ? ? ? FileStream file = new FileStream(strExcelFileName, FileMode.OpenOrCreate);
? ? ? ? workbook.Write(file);
? ? ? ? file.Flush();
? ? ? ? file.Close();
? ? ? ? return true;
? ? }
? ? catch (Exception ex)
? ? {
? ? ? ? return false;
? ? }
}


/// <summary>
/// 將excel中的數(shù)據(jù)導(dǎo)入到DataTable中
/// </summary>
/// <param name="fileName">fileName</param>
/// <param name="sheetName">excel工作薄sheet的名稱(chēng)</param>
/// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>
/// <returns>返回的DataTable</returns>
public DataTable ExcelToDataTable(string fileName, string sheetName, bool isFirstRowColumn)
{
? ? ISheet sheet = null;
? ? DataTable data = new DataTable();
? ? IWorkbook workbook = null;
? ? FileStream fs = null;
? ? int startRow = 0;
? ? try
? ? {
? ? ? ? fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
? ? ? ? if (fileName.IndexOf(".xlsx") > 0) // 2007版本
? ? ? ? ? ? workbook = new XSSFWorkbook(fs);
? ? ? ? else if (fileName.IndexOf(".xls") > 0) // 2003版本
? ? ? ? ? ? workbook = new HSSFWorkbook(fs);

? ? ? ? if (sheetName != null)
? ? ? ? {
? ? ? ? ? ? sheet = workbook.GetSheet(sheetName);
? ? ? ? ? ? if (sheet == null) //如果沒(méi)有找到指定的sheetName對(duì)應(yīng)的sheet,則嘗試獲取第一個(gè)sheet
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sheet = workbook.GetSheetAt(0);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? sheet = workbook.GetSheetAt(0);
? ? ? ? }
? ? ? ? if (sheet != null)
? ? ? ? {
? ? ? ? ? ? IRow firstRow = sheet.GetRow(0);
? ? ? ? ? ? int cellCount = firstRow.LastCellNum; //一行最后一個(gè)cell的編號(hào) 即總的列數(shù)

? ? ? ? ? ? if (isFirstRowColumn)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ICell cell = firstRow.GetCell(i);
? ? ? ? ? ? ? ? ? ? if (cell != null)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? string cellValue = cell.StringCellValue;
? ? ? ? ? ? ? ? ? ? ? ? if (cellValue != null)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? DataColumn column = new DataColumn(cellValue);
? ? ? ? ? ? ? ? ? ? ? ? ? ? data.Columns.Add(column);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? startRow = sheet.FirstRowNum + 1;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? startRow = sheet.FirstRowNum;
? ? ? ? ? ? }

? ? ? ? ? ? //最后一列的標(biāo)號(hào)
? ? ? ? ? ? int rowCount = sheet.LastRowNum;
? ? ? ? ? ? for (int i = startRow; i <= rowCount; ++i)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? IRow row = sheet.GetRow(i);
? ? ? ? ? ? ? ? if (row == null) continue; //沒(méi)有數(shù)據(jù)的行默認(rèn)是null       

? ? ? ? ? ? ? ? DataRow dataRow = data.NewRow();
? ? ? ? ? ? ? ? for (int j = row.FirstCellNum; j < cellCount; ++j)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (row.GetCell(j) != null) //同理,沒(méi)有數(shù)據(jù)的單元格都默認(rèn)是null
? ? ? ? ? ? ? ? ? ? ? ? dataRow[j] = row.GetCell(j).ToString();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? data.Rows.Add(dataRow);
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? return data;
? ? }
? ? catch (Exception ex)
? ? {
? ? ? ? Console.WriteLine("Exception: " + ex.Message);
? ? ? ? return null;
? ? }
}

3、使用

(1)DataTable寫(xiě)入Excel

var Sql = "select id,name from test";
var dt = DB.DB.ExecuteDataTable(Sql);
//保存文件
string saveName = "temp_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
string DownloadExcelDic = "DownloadExcel";
string webDic = AppDomain.CurrentDomain.BaseDirectory + DownloadExcelDic;
if (!Directory.Exists(webDic))//判斷目錄是否存在
{
? ? Directory.CreateDirectory(webDic);//不存在則創(chuàng)建新目錄
}
string path = webDic + @"\" + saveName; //目錄+文件名+后綴名
bool isBuild = GridToExcelByNPOI(dt, path);

(2)導(dǎo)入Excel返回DataTable

//1、先上傳文件,將文件保存到服務(wù)器上

//2、導(dǎo)入Excel
string fileDic = "";//文件路徑
var dt = ExcelToDataTable(fileDic, "sheet1", true);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#通過(guò)接口與線程通信(捕獲線程狀態(tài))示例代碼

    C#通過(guò)接口與線程通信(捕獲線程狀態(tài))示例代碼

    本文介紹C#通過(guò)接口與線程通信(捕獲線程狀態(tài)),并提供簡(jiǎn)單的示例代碼供參考
    2013-12-12
  • C#圖表算法之有向圖

    C#圖表算法之有向圖

    這篇文章介紹了C#圖表算法之有向圖,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • c#冒泡排序算法示例

    c#冒泡排序算法示例

    這篇文章主要介紹了c#冒泡排序算法示例,需要的朋友可以參考下
    2014-04-04
  • C#中abstract的用法詳解

    C#中abstract的用法詳解

    abstract可以用來(lái)修飾類(lèi),方法,屬性,索引器和時(shí)間,這里不包括字段. 使用abstrac修飾的類(lèi),該類(lèi)只能作為其他類(lèi)的基類(lèi),不能實(shí)例化,而且abstract修飾的成員在派生類(lèi)中必須全部實(shí)現(xiàn),不允許部分實(shí)現(xiàn),否則編譯異常
    2016-12-12
  • 使用C#獲取系統(tǒng)特殊文件夾路徑的解決方法

    使用C#獲取系統(tǒng)特殊文件夾路徑的解決方法

    本篇文章是對(duì)使用C#獲取系統(tǒng)特殊文件夾路徑的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#清除字符串內(nèi)空格的方法

    C#清除字符串內(nèi)空格的方法

    這篇文章主要介紹了C#清除字符串內(nèi)空格的方法,是C#操作字符串非常實(shí)用的技巧,需要的朋友可以參考下
    2014-10-10
  • C#實(shí)體對(duì)象序列化成Json并讓字段的首字母小寫(xiě)的兩種解決方法

    C#實(shí)體對(duì)象序列化成Json并讓字段的首字母小寫(xiě)的兩種解決方法

    這篇文章主要介紹了C#實(shí)體對(duì)象序列化成Json并讓字段的首字母小寫(xiě)的兩種方法,在這兩種方法中小編比較推薦使用第二種方法,需要的朋友可以參考下
    2018-06-06
  • 淺析C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別

    淺析C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別

    C#靜態(tài)方法與非靜態(tài)方法的區(qū)別不僅僅是概念上的,那么他們有什么具體的區(qū)別呢?讓我們通過(guò)本文向大家介紹下C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別,一起看看吧
    2017-09-09
  • WinForm ToolTip使用方法小結(jié)

    WinForm ToolTip使用方法小結(jié)

    這篇文章主要介紹了WinForm ToolTip使用方法小結(jié),對(duì)C#初學(xué)者有一定的借鑒參考價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C#面向?qū)ο筇卣鞯木唧w實(shí)現(xiàn)及作用詳解

    C#面向?qū)ο筇卣鞯木唧w實(shí)現(xiàn)及作用詳解

    所有的面相對(duì)象思想,歸根結(jié)底是為了簡(jiǎn)化代碼,減少代碼量,構(gòu)建更符合現(xiàn)實(shí)生活邏輯的程序代碼,從而減輕程序員的負(fù)擔(dān)。不能一味地或者說(shuō)刻意地去使用面相對(duì)象的思想而忽略了程序所實(shí)現(xiàn)的功能或者框架,要根據(jù)實(shí)際情況
    2013-10-10

最新評(píng)論