最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

1、準(zhǔn)備工作

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

2、幫助類

/// <summary>
/// DataTable寫入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來看
? ? ? ? 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);
? ? ? ? }

? ? ? ? //寫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的名稱</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) //如果沒有找到指定的sheetName對應(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的編號 即總的列數(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)號
? ? ? ? ? ? int rowCount = sheet.LastRowNum;
? ? ? ? ? ? for (int i = startRow; i <= rowCount; ++i)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? IRow row = sheet.GetRow(i);
? ? ? ? ? ? ? ? if (row == null) continue; //沒有數(shù)據(jù)的行默認(rèn)是null       

? ? ? ? ? ? ? ? DataRow dataRow = data.NewRow();
? ? ? ? ? ? ? ? for (int j = row.FirstCellNum; j < cellCount; ++j)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (row.GetCell(j) != null) //同理,沒有數(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寫入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);

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

相關(guān)文章

  • c#實(shí)現(xiàn)將pdf轉(zhuǎn)文本的示例分享

    c#實(shí)現(xiàn)將pdf轉(zhuǎn)文本的示例分享

    這篇文章主要介紹了c#實(shí)現(xiàn)將pdf轉(zhuǎn)文本的示例,需要的朋友可以參考下
    2014-03-03
  • C#生成本地配置文件的實(shí)現(xiàn)示例

    C#生成本地配置文件的實(shí)現(xiàn)示例

    本文將介紹如何使用C#語言生成本地配置文件,以便為應(yīng)用程序提供靈活的配置選項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • C#飛機(jī)打字游戲的代碼示例(winform版)

    C#飛機(jī)打字游戲的代碼示例(winform版)

    這篇文章主要介紹了C#飛機(jī)打字游戲的代碼示例(winform版),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 輸出的文本實(shí)現(xiàn)對齊的方法(超簡單)

    輸出的文本實(shí)現(xiàn)對齊的方法(超簡單)

    下面小編就為大家分享一篇c#輸出的文本實(shí)現(xiàn)對齊的方法,特別簡單!希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • C#調(diào)用AForge實(shí)現(xiàn)攝像頭錄像的示例代碼

    C#調(diào)用AForge實(shí)現(xiàn)攝像頭錄像的示例代碼

    這篇文章主要介紹了C#調(diào)用AForge實(shí)現(xiàn)攝像頭錄像的示例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-09-09
  • python實(shí)現(xiàn)AutoResetEvent類的阻塞模式方法解析

    python實(shí)現(xiàn)AutoResetEvent類的阻塞模式方法解析

    AutoResetEvent :當(dāng)某個(gè)線程執(zhí)行到WaitOne()方法時(shí),該線程則會(huì)處于阻塞模式,當(dāng)被調(diào)用了Set()方法,阻塞的線程則會(huì)繼續(xù)向下執(zhí)行,其狀態(tài)立即被自動(dòng)設(shè)置為阻塞模式
    2012-11-11
  • C#通過GET/POST方式發(fā)送Http請求

    C#通過GET/POST方式發(fā)送Http請求

    本文主要介紹了C#實(shí)現(xiàn)http請求的兩種方式,get和post方式。文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • c#并行任務(wù)多種優(yōu)化方案分享(異步委托)

    c#并行任務(wù)多種優(yōu)化方案分享(異步委托)

    c#并行任務(wù)多種優(yōu)化方案分享,使用異步委托+回調(diào)函數(shù)方式實(shí)現(xiàn),大家參考使用吧
    2013-12-12
  • 關(guān)于c#二叉樹的實(shí)現(xiàn)

    關(guān)于c#二叉樹的實(shí)現(xiàn)

    本篇文章小編為大家介紹,關(guān)于c#二叉樹的實(shí)現(xiàn)。需要的朋友參考下
    2013-04-04
  • 使用C#編寫15子游戲

    使用C#編寫15子游戲

    這篇文章主要為大家詳細(xì)介紹了使用C#編寫15子游戲的具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05

最新評論

浙江省| 六盘水市| 九龙城区| 嵩明县| 铁岭县| 昭平县| 金塔县| 阿克陶县| 大安市| 开封市| 内黄县| 永福县| 金川县| 贺州市| 崇州市| 北碚区| 正宁县| 武冈市| 铁岭县| 施甸县| 赣榆县| 冀州市| 米泉市| 湘潭县| 兰坪| 吴堡县| 儋州市| 长宁区| 邢台县| 新蔡县| 丹东市| 龙州县| 苗栗县| 林州市| 云南省| 巍山| 郎溪县| 永靖县| 旅游| 扶沟县| 永福县|