public  static  void WriteExcel ( ) 
{ string =  @"F:\12312\excel.xlsx" ;  string =  @"F:\12312\" + DateTime.Now.ToString(" yyyyMMddHHmmssfff") + " . xlsx"; string =  @"F:\12312\test.jpg" ; IWorkbook  workbook =  ExcelHelper. GetReadWorkbook ( templateFile) ; ISheet  sheet =  workbook. GetSheetAt ( 0 ) ; try { ExcelHelper. SetCellValue ( sheet,  20 ,  0 ,  "这里是第1行第1列内容" ) ; ExcelHelper. SetCellValue ( sheet,  0 ,  1 ,  "这里是第1行第2列内容" ) ; ExcelHelper. SetCellValue ( sheet,  1 ,  0 ,  "这里是第2行第1列内容" ) ; ExcelHelper. SetCellValue ( sheet,  1 ,  1 ,  "这里是第2行第2列内容" ) ; sheet. GetRow ( 1 ) . Height =  44  *  20 ;  sheet. SetColumnWidth ( 1 ,  50  *  256 ) ;  ExcelHelper. SetCellValue ( sheet,  2 ,  0 ,  "这里是第3行第1列内容,需要设置字体样式" ) ; ExcelHelper. SetCellRangeAddress ( sheet,  2 ,  5 ,  0 ,  3 ) ; ExcelHelper. AddRengionBorder ( workbook,  sheet,  2 ,  5 ,  0 ,  3 ) ; var =  ( Bitmap) Image. FromFile ( "1.bmp" ) ; ExcelHelper. InsertImage ( workbook,  sheet,  7 ,  16 ,  0 ,  2 ,  bitmap) ; ExcelHelper. Save ( workbook,  outFile) ; Process. Start ( outFile) ; } catch  ( Exception  ex) { throw  ex; } 
} using  System ; 
using  System. Collections. Generic ; 
using  System. Data ; 
using  System. Drawing ; 
using  System. Drawing. Imaging ; 
using  System. IO ; 
using  System. Linq ; 
using  NPOI. HSSF. UserModel ; 
using  NPOI. SS. UserModel ; 
using  NPOI. SS. Util ; 
using  NPOI. XSSF. UserModel ; namespace  Demo_Excel 
{ public  class  ExcelHelper { public  static  IWorkbook  GetReadWorkbook ( string ) { FileStream  fs =  File. Open ( filename,  FileMode. Open,  FileAccess. Read,  FileShare. ReadWrite) ; IWorkbook  workbook; string =  Path. GetExtension ( filename) . ToLower ( ) ; switch  ( fileExt) { case  ".xlsx" : workbook =  new  XSSFWorkbook ( fs) ; break ; case  ".xls" : workbook =  new  HSSFWorkbook ( fs) ; break ; default : throw  new  Exception ( "不支持的文件类型" ) ; } fs. Close ( ) ; return  workbook; } public  static  IWorkbook  GetWriteWorkbook ( string ) { if  ( string . IsNullOrWhiteSpace ( filename) ) throw  new  Exception ( "不支持的文件类型" ) ; string =  Path. GetExtension ( filename) . ToLower ( ) ; switch  ( fileExt) { case  ".xlsx" :  return  new  XSSFWorkbook ( ) ; case  ".xls" :  return  new  HSSFWorkbook ( ) ; default :  throw  new  Exception ( "不支持的文件类型" ) ; } } public  static  void Save ( IWorkbook  workbook,  string ) { MemoryStream  stream =  new  MemoryStream ( ) ; workbook. Write ( stream) ; var =  stream. ToArray ( ) ; using  ( FileStream  fs =  new  FileStream ( filename,  FileMode. Create,  FileAccess. Write) ) { fs. Write ( buf,  0 ,  buf. Length) ; fs. Flush ( ) ; fs. Close ( ) ; } } public  static  int UpdataExcel ( string ,  string ,  int ,  Dictionary< string ,  string >   dictionary) { var =  dictionary. Keys. ToArray ( ) ; IWorkbook  workbook =  GetReadWorkbook ( sourcefile) ; ISheet  sheet =  workbook. GetSheetAt ( sheetIndex) ; int =  sheet. LastRowNum; for  ( int =  0 ;  i <  endRow;  i++ ) { var =  sheet. GetRow ( i) ; for  ( int =  0 ;  j <  row. LastCellNum;  j++ ) { var =  GetCellString ( row. GetCell ( j) ) ; if  ( allKeys. Contains ( data) ) { row. Cells[ j] . SetCellValue ( dictionary[ data] ) ; } } } Save ( workbook,  outfile) ; return  0 ; } public  static  int UpdataExcel ( string ,  string ,  int ,  Dictionary< Point,  string >   dictionary) { IWorkbook  workbook =  GetReadWorkbook ( sourcefile) ; ISheet  sheet =  workbook. GetSheetAt ( sheetIndex) ; foreach  ( var in  dictionary. Keys) { SetCellValue ( sheet,  key. X,  key. Y,  dictionary[ key] ) ; } Save ( workbook,  outfile) ; return  0 ; } public  static  int Write ( string ,  DataTable  data,  string ,  bool ) { try { IWorkbook  workbook =  GetWriteWorkbook ( fileName) ; ISheet  sheet =  workbook. CreateSheet ( sheetName) ; int =  0 ; if  ( isColumnWritten) { IRow  rowHeader =  sheet. CreateRow ( count++ ) ; for  ( int =  0 ;  i <  data. Columns. Count;  i++ ) { ICell  cell =  rowHeader. CreateCell ( i) ; cell. SetCellValue ( data. Columns[ i] . ColumnName) ; } } for  ( int =  0 ;  i <  data. Rows. Count;  i++ ) { IRow  rowData =  sheet. CreateRow ( count++ ) ; for  ( int =  0 ;  j <  data. Columns. Count;  j++ ) { ICell  cell =  rowData. CreateCell ( j) ; cell. SetCellValue ( data. Rows[ i] [ j ] . ToString ( ) ) ; } } Save ( workbook,  fileName) ; return  count; } catch  ( Exception  ex) { return  - 1 ; } } public  static  int Write ( string ,  DataTable  data,  bool ) { int =  Write ( fileName,  data,  "Sheet1" ,  isColumnWritten) ; return  ret; } public  static  int Write ( string ,  DataTable  data) { int =  Write ( fileName,  data,  true ) ; return  ret; } public  static  int Read ( string ,  int ,  bool ,  out  DataTable  data) { data =  new  DataTable ( ) ; try { IWorkbook  workbook =  GetReadWorkbook ( fileName) ; ISheet  sheet =  workbook. GetSheetAt ( sheetIndex) ; if  ( isFirstRowCellName) { IRow  firstRow =  sheet. GetRow ( 0 ) ; var =  ReadDataRow ( firstRow) ; data. Columns. AddRange ( list. Select ( t =>  new  DataColumn ( t) ) . ToArray ( ) ) ; } else { int =  0 ; for  ( int =  0 ;  i <  sheet. LastRowNum;  i++ ) { nMaxCol =  Math. Max ( nMaxCol,  sheet. GetRow ( i) . LastCellNum) ; } for  ( int =  0 ;  i <  nMaxCol;  i++ ) { data. Columns. Add ( $"列 { i +  1  } " ) ; } } int =  ! isFirstRowCellName ?  0  :  1 ; int =  sheet. LastRowNum; var =  Read ( sheet,  startRow,  endRow,  ref  data) ; if  ( ret2 <  0 )  return  - 1 ; return  data. Rows. Count; } catch  ( Exception  ex) { throw  ex; } } public  static  int Read ( string ,  string ,  bool ,  out  DataTable  data) { data =  new  DataTable ( ) ; try { IWorkbook  workbook =  GetReadWorkbook ( fileName) ; ISheet  sheet =  workbook. GetSheet ( sheetName) ; Console. WriteLine ( sheet. SheetName) ; if  ( isFirstRowCellName) { IRow  firstRow =  sheet. GetRow ( 0 ) ; var =  ReadDataRow ( firstRow) ; data. Columns. AddRange ( list. Select ( t =>  new  DataColumn ( t) ) . ToArray ( ) ) ; } else { int =  0 ; for  ( int =  0 ;  i <  sheet. LastRowNum;  i++ ) { nMaxCol =  Math. Max ( nMaxCol,  sheet. GetRow ( i) . LastCellNum) ; } for  ( int =  0 ;  i <  nMaxCol;  i++ ) { data. Columns. Add ( $"列 { i +  1  } " ) ; } } int =  ! isFirstRowCellName ?  0  :  1 ; int =  ! isFirstRowCellName ?  0  :  1 ; var =  Read ( sheet,  startRow,  endRow,  ref  data) ; if  ( ret <  0 ) return  - 1 ; return  data. Rows. Count; } catch  ( Exception  ex) { return  - 1 ; } } public  static  int Read ( string ,  bool ,  out  DataTable  data) { int =  Read ( fileName,  "sheet1" ,  isFirstRowCellName,  out  data) ; return  ret; } public  static  int Read ( string ,  out  DataTable  data) { int =  Read ( fileName,  "sheet1" ,  false ,  out  data) ; return  ret; } public  static  int Read ( ISheet  sheet,  int ,  int ,  ref  DataTable  data) { endRow +=  1 ; for  ( int =  startRow;  i <  endRow;  i++ ) { var =  sheet. GetRow ( i) ; if  ( sheetRow ==  null ) { data. Rows. Add ( ) ; } else { var =  ReadDataRow ( sheetRow) ; var =  data. NewRow ( ) ; int =  Math. Min ( list. Count,  data. Columns. Count) ; for  ( int =  0 ;  j <  count;  j++ ) { row[ j]  =  list[ j] ; } data. Rows. Add ( row) ; } } return  data. Rows. Count; } public  static  List< string >   ReadDataRow ( ISheet  sheet,  int )  =>  ReadDataRow ( sheet. GetRow ( index) ) ; public  static  List< string >   ReadDataRow ( IRow  row) { List< string >   result =  null ; if  ( row !=  null ) { result =  new  List< string >  ( ) ; int =  0 ; int =  row. LastCellNum; for  ( int =  startColumn;  i <  endColumn;  i++ ) { result. Add ( GetCellString ( row. GetCell ( i) ) ) ; } } return  result; } public  static  void InsertImage ( IWorkbook  workbook,  ISheet  sheet,  int ,  int ,  int ,  int ,  Bitmap  bitmap) { byte [ ] =  BitmapToBytes ( bitmap) ; int =  workbook. AddPicture ( imgBytes,  PictureType. PNG) ; if  ( workbook is  XSSFWorkbook ) { XSSFDrawing  patriarch =  ( XSSFDrawing) sheet. CreateDrawingPatriarch ( ) ; XSSFClientAnchor  anchor =  new  XSSFClientAnchor ( 10 ,  10 ,  0 ,  0 ,  firstCell,  firstRow,  lastCell,  lastRow) ; XSSFPicture  pict =  ( XSSFPicture) patriarch. CreatePicture ( anchor,  pictureIdx) ; } else { HSSFPatriarch  patriarch =  ( HSSFPatriarch) sheet. CreateDrawingPatriarch ( ) ; HSSFClientAnchor  anchor =  new  HSSFClientAnchor ( 10 ,  10 ,  0 ,  0 ,  firstCell,  firstRow,  lastCell,  lastRow) ; HSSFPicture  pict =  ( HSSFPicture) patriarch. CreatePicture ( anchor,  pictureIdx) ; } } public  static  void SetCellValue ( ISheet  sheet,  int ,  int ,  string value ) { if  ( sheet. GetRow ( rowIndex)  ==  null ) { sheet. CreateRow ( rowIndex) ; } if  ( sheet. GetRow ( rowIndex) . GetCell ( cellIndex)  ==  null ) { sheet. GetRow ( rowIndex) . CreateCell ( cellIndex) ; } sheet. GetRow ( rowIndex) . GetCell ( cellIndex) . SetCellValue ( value ) ; } public  static  void SetCellRangeAddress ( ISheet  sheet,  int ,  int ,  int ,  int ) { for  ( int =  rowstart;  r <=  rowend;  r++ ) { for  ( int =  colstart;  c <=  colend;  c++ ) { if  ( sheet. GetRow ( r)  ==  null ) { sheet. CreateRow ( r) ;  } if  ( sheet. GetRow ( r) . GetCell ( c)  ==  null ) { sheet. GetRow ( r) . CreateCell ( c) ;  } } } CellRangeAddress  cellRangeAddress =  new  CellRangeAddress ( rowstart,  rowend,  colstart,  colend) ; sheet. AddMergedRegion ( cellRangeAddress) ; } public  static  void AddRengionBorder ( IWorkbook  workbook,  ISheet  sheet,  int ,  int ,  int ,  int ) { for  ( int =  firstRow;  i <  lastRow;  i++ ) { for  ( int =  firstCell;  n <  lastCell;  n++ ) { ICell  cell; cell =  sheet. GetRow ( i) . GetCell ( n) ; if  ( cell ==  null ) { cell =  sheet. GetRow ( i) . CreateCell ( n) ; } ICellStyle  style =  sheet. Workbook. CreateCellStyle ( ) ; style. BorderTop =  BorderStyle. Thin; style. BorderBottom =  BorderStyle. Thin; style. BorderLeft =  BorderStyle. Thin; style. BorderRight =  BorderStyle. Thin; style. Alignment =  HorizontalAlignment. Center;    style. VerticalAlignment =  VerticalAlignment. Center;  if  ( i ==  firstRow)  { style. FillForegroundColor =  NPOI. HSSF. Util. HSSFColor. Black. Index;  style. FillPattern =  FillPattern. SolidForeground; IFont  font =  workbook. CreateFont ( ) ;  font. Color =  NPOI. HSSF. Util. HSSFColor. White. Index;   font. FontHeightInPoints =  18 ; style. SetFont ( font) ;  } cell. CellStyle =  style; } } } private  static  byte [ ] BitmapToBytes ( Bitmap  bitmap) { MemoryStream  ms =  new  MemoryStream ( ) ; bitmap. Save ( ms,  ImageFormat. Bmp) ; ms. Seek ( 0 ,  SeekOrigin. Begin) ; byte [ ] =  new  byte [ ms. Length] ; ms. Read ( bytes,  0 ,  bytes. Length) ; ms. Dispose ( ) ; return  bytes; } private  static  string GetCellString ( ICell  cell) { if  ( cell !=  null ) { switch  ( cell. CellType) { case  CellType. Unknown: return  "" ; case  CellType. Numeric: return  cell. NumericCellValue. ToString ( ) ; case  CellType. String: return  cell. StringCellValue; case  CellType. Formula: return  cell. CellFormula; case  CellType. Blank: return  "" ; case  CellType. Boolean: return  cell. BooleanCellValue. ToString ( ) ; case  CellType. Error: return  "" ; default : return  "" ; } } else { return  "" ; } } } }