NPOI 常用方法封裝

2022-03-29 02:13:40 字數 3346 閱讀 7913

/// /// 工作表轉list

///

/// 需要轉換的泛型類

/// 工作表

/// 起始索引

///

public static listsheettolist(isheet sheet, int rowstartindex = 1) where t : class, new()

if (row == null || row.rownum < rowstartindex) continue;

var t = activator.createinstance(typeof(t)) as t;

var props = t.gettype().getproperties().toarray();

for (var i = 0; i < row.lastcellnum; i++)

list.add(t);

}return list;

} /// /// 獲取單元格的值

///

///

///

///

private static object getcellvalue(type valuetype, string value)

else if (valuetype == typeof(float))

else if (valuetype == typeof(datetime))

else if (valuetype == typeof(decimal))

else

}// 對應擴充套件

/// /// 將資料表轉為泛型物件

///

/// 與工作表列對應數量的泛型實體類

/// 休要轉換工作表

/// 工作表中有效資料的起始索引

///

public static listtrytranstolist(this isheet sheet, int rowindex = 0) where t : class,new()

catch (exception e)

}

說明:目前只支援的泛型類是根據工作表的實際模板生成,對複雜的表頭還需擴充套件處理

/// /// 將**轉為流

///

///

///

public static byte converttobytes(this iworkbook workbook)

catch (exception ex);}

}

/// /// 給指定工作表新增資訊列

///

/// 資訊表

/// 錯誤資訊

/// 列標題

/// 列標題行起始索引

/// 標記行的背景色

/// 標記行的前景色

///

public static stream addinfocolumn(

isheet sheet,

listinfos,

string coltitle,

int titlerowindex=0,

short backgroundcolor = hssfcolor.red.index,

short foregroundcolor = hssfcolor.white.index

)); return sheet.converttostream();

}catch (exception e)

}/// /// 設定單元格樣式

///

///

///

///

private static void setcellstyle(

irow row,

short backgroundcolor=hssfcolor.red.index,

short foregroundcolor=hssfcolor.white.index)

}

/// /// 獲取工作表空間

///

/// 檔案流

/// 檔名稱

///

public static iworkbook getworkbookbystream(stream stream,string filename)

else if (filename.endswith(".xls"))

else

}

private static void setcellstyle(

irow row)

if (row == null) return;

foreach (var cell in row.cells)

}

/// /// 獲取工作表的了型別

///

///

///

public static excelfiletype getworkbooktype(this isheet sheet)

}

public static stream readfilestream(this fileuploaddto filedto) => filedto.file?.openreadstream();
/// /// 將檔案dto 轉換成工作表

/// or ///

/// 上傳的檔案

///

public static iworkbook converttoworkbook(fileuploaddto filedto)

catch (exception e)

}

/// /// 檢查轉換後集合是否為空

///

///

///

///

public static listchecknull(this listlist)=>

list ?? throw new argumentnullexception(paramname: nameof(checknull), "未將物件例項化");

/// /// 檢查檔案流是否為空

///

///

///

public static stream checknull(this stream stream) =>

stream ?? throw new argumentnullexception(paramname: nameof(stream),"檔案流為空");

Npoi常用操作方法介紹

1.shiftrows startrow,endrow,moverows 將開始行到結束行向上或者向下移動moverows行,moverows為正數向下移動,為負數向上移動 向上移動,會把之前的行覆蓋掉,所以想刪除某行往往通過向上移動來實現刪除效果 eg 將第1行到第5行向下移動一行 sheet.s...

常用日期封裝方法

目錄 判斷時間是否在時間段內 判斷時間是否在時間段內 param nowtime 需要判斷的時間 param begintime param endtime return public static boolean belongcalendar date nowtime,date begintime...

php常用方法封裝

public function consumer request request elseif msg err else public function producer request request file name test.csv ob end clean header content t...