NPOI datagridatagriexport EXCEL

Source: Internet
Author: User

NPOI official website http://npoi.codeplex.com/load NPOI. DLL, reference these two namespaces

Using NPOI. HSSF. UserModel;
Using NPOI. SS. UserModel;

# Region NPOI datagridatagriexport EXCEL /// <summary> // NPOI DataGridView export EXCEL /// </summary> /// <param name = "fileName"> Save the file name by default </param> /// <param name = "dgv"> DataGridView </param> /// <param name = "fontname"> fontname </param> /// <param name = "fontsize"> font size </param> public void ExportExcel (string fileName, dataGridView dgv, string fontname, short fontsize) {// checks whether data exists if (dgv. selectedRows. count = 0) re Turn; // create the main object HSSFWorkbook workbook = new HSSFWorkbook (); HSSFSheet sheet = (HSSFSheet) workbook. createSheet ("Weight"); // you can specify the font, size, and alignment of HSSFCellStyle = (HSSFCellStyle) workbook. createCellStyle (); HSSFFont font = (HSSFFont) workbook. createFont (); font. fontName = fontname; font. fontHeightInPoints = fontsize; style. setFont (font); style. alignment = NPOI. SS. userModel. horizontalAlignment. center; // Center Align // Add the header HSSFRow dataRow = (HSSFRow) sheet. createRow (0); for (int I = 0; I <dgv. columns. count; I ++) {dataRow. createCell (I ). setCellValue (dgv. columns [I]. headerText); dataRow. getCell (I ). cellStyle = style;} // This line of comment is set to filter // sheet. setAutoFilter (new CellRangeAddress (0, dgv. columns. count, 0, dgv. columns. count); // Add columns and content for (int I = 0; I <dgv. rows. count; I ++) {dataRow = (HSSFRow) sheet. createRow (I + 1); for (int j = 0; j <dgv. columns. count; j ++) {string ValueType = dgv. rows [I]. cells [j]. value. getType (). toString (); string Value = dgv. rows [I]. cells [j]. value. toString (); switch (ValueType) {case "System. string ": // String type dataRow. createCell (j ). setCellValue (Value); break; case "System. dateTime ": // Date type System. dateTime dateV; System. dateTime. tryParse (Value, out dateV); dataRow. createCell (j ). setCellVa Lue (dateV); break; case "System. boolean ": // Boolean bool boolV = false; bool. tryParse (Value, out boolV); dataRow. createCell (j ). setCellValue (boolV); break; case "System. int16 ": // integer case" System. int32 ": case" System. int64 ": case" System. byte ": int intV = 0; int. tryParse (Value, out intV); dataRow. createCell (j ). setCellValue (intV); break; case "System. decimal ": // floating case" System. double ": double doubV = 0; d Ouble. tryParse (Value, out doubV); dataRow. createCell (j ). setCellValue (doubV); break; case "System. DBNull ": // processing dataRow for null values. createCell (j ). setCellValue (""); break; default: dataRow. createCell (j ). setCellValue (""); break;} dataRow. getCell (j ). cellStyle = style; // set the width of sheet. setColumnWidth (j, (Value. length + 10) * 256) ;}// save the file string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog (); SaveDialog. defaultExt = "xls"; saveDialog. filter = "Excel file | *. xls "; saveDialog. fileName = fileName; MemoryStream MS = new MemoryStream (); if (saveDialog. showDialog () = DialogResult. OK) {saveFileName = saveDialog. fileName; if (! CheckFiles (saveFileName) {MessageBox. show ("the file is used. close the file" + saveFileName); workbook = null; ms. close (); ms. dispose (); return;} workbook. write (MS); FileStream file = new FileStream (saveFileName, FileMode. create); workbook. write (file); file. close (); workbook = null; ms. close (); ms. dispose (); MessageBox. show (fileName + "saved successfully", "prompt", MessageBoxButtons. OK);} else {workbook = null; ms. close (); ms. di Spose () ;}# endregion # region detection file occupied [DllImport ("kernel32.dll")] public static extern IntPtr _ lopen (string lpPathName, int iReadWrite ); [DllImport ("kernel32.dll")] public static extern bool CloseHandle (IntPtr hObject); public const int OF_READWRITE = 2; public const int OF_SHARE_DENY_NONE = 0x40; public readonly IntPtr HFILE_ERROR = new IntPtr (-1); // <summary> // check if the file is occupied /// </summary> /// <Param name = "FileNames"> file path to be detected </param> // <returns> </returns> public bool CheckFiles (string FileNames) {if (! File. exists (FileNames) {// return true if the file does not exist;} IntPtr vHandle = _ lopen (FileNames, OF_READWRITE | OF_SHARE_DENY_NONE); if (vHandle = HFILE_ERROR) {// return false;} // CloseHandle (vHandle); return true;} # endregion

The specific effect is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.