Home > Net >  From the DataGridView import excel code, but excel column width will not change.
From the DataGridView import excel code, but excel column width will not change.

Time:11-12

Need to DataGridView import excel, the code is as follows:
 public static void DataGridViewToExcel (DataGridView DGV) 
{


# region to verify operability

//that save dialog
=new SaveFileDialog DLG SaveFileDialog ();
//silence file suffix
DLG. DefaultExt="XLSX";
//file suffix list
DLG. Filter="EXCEL file (*. XLSX) | *. XLSX";
//silent path is the current system path
DLG. InitialDirectory=Directory. GetCurrentDirectory ();
//open the save dialog
If (DLG. ShowDialog ()==DialogResult. Cancel) return;
//return path to the file
String fileNameString=DLG. FileName;
//verification strFileName for null or invalid values
If (fileNameString. The Trim ()=="")
{return; }
//define rows and columns of data in the form
Int rowscount=DGV. Rows. Count;
Int colscount=DGV. Columns. The Count;
//the number of rows must be greater than 0
If (rowscount & lt;=0)
{
MessageBox. Show (" no data available to save ", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information);
return;
}

//the number of columns must be greater than 0
If (colscount & lt;=0)
{
MessageBox. Show (" no data available to save ", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information);
return;
}

//line number can not be greater than 65536
If (rowscount & gt; 65536)
{
MessageBox. Show (" the number of data records too much (not more than 65536), cannot be saved ", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information);
return;
}

//the number of columns may not be greater than 255
If (colscount & gt; 255)
{
MessageBox. Show (" data record number of lines is too much, can't save ", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information);
return;
}

//verification to fileNameString named file exists, if there is a delete it
The FileInfo file=new the FileInfo (fileNameString);
If (the file. The Exists)
{
Try
{
File. The Delete ();
}
The catch (Exception error)
{
MessageBox. Show (the error Message, "delete failed," MessageBoxButtons. OK, MessageBoxIcon. Warning).
return;
}
}
# endregion
Microsoft. Office. Interop. Excel. Application objExcel=null;
. Microsoft. Office. Interop. Excel Workbook objWorkbook=null;
. Microsoft. Office. Interop. Excel Worksheet objsheet=null;

Try
{
//statement object
ObjExcel=new Microsoft. Office. Interop. Excel. The Application ();
ObjWorkbook=objExcel. Workbooks. The Add (Value) System. Reflection. Missing.;
//objsheet=(. Microsoft. Office. Interop. Excel Worksheet) objWorkbook. ActiveSheet;
Objsheet=(. Microsoft. Office. Interop. Excel Worksheet) objWorkbook. Worksheets [1].

//set the EXCEL invisible
ObjExcel. Visible=false;

//to Excel in the written form of header
Int displayColumnsCount=1;
for (int i=0; I & lt;=DGV. ColumnCount - 1; I++)
{
If (DGV. Columns [I] Visible==true)
{
ObjExcel. Cells [1, displayColumnsCount]=DGV. Columns [I] the HeaderText. The Trim ();
DisplayColumnsCount++;
}
}
//set the progress bar
//tempProgressBar. Refresh ();
//tempProgressBar. Visible=true;
//tempProgressBar. Minimum=1;
//tempProgressBar. Maximum=DGV. RowCount;
//tempProgressBar. Step=1;
//to Excel in line by line by line data are listed in a written form
For (int row=0; The row & lt;=DGV. RowCount - 1; Row++)
{
//tempProgressBar PerformStep ();

DisplayColumnsCount=1;
For (int col=0; Col & lt; Colscount; Col++)
{
If (DGV. Columns [col]. Visible==true)
{
Try
{
ObjExcel. Cells [row + 2, displayColumnsCount]=DGV. Rows [r]. Row Cells [col] Value. The ToString (). The Trim ();
DisplayColumnsCount++;
}
The catch (Exception)
{

}

}
}
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- to adjust column width -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --//
Objsheet.. The Columns [1]. ColumnWidth=15;// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (error)



//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --//
//hide the progress bar
//tempProgressBar. Visible=false;
//save the file
ObjWorkbook. SaveAs (fileNameString, System. Reflection. Missing. The Value, System. Reflection. Missing. The Value, System. Reflection. Missing. The Value, System. Reflection. Missing. The Value,
System. Reflection. Missing. The Value, Microsoft Office, Interop. Excel. XlSaveAsAccessMode. XlShared, System. Reflection. Missing. The Value, System. Reflection. Missing. The Value, System. Reflection. Missing. The Value,
System. Reflection. Missing. The Value, System. Reflection. Missing. Value);
}
The catch (Exception error)
{
MessageBox. Show (the error Message, "Warning", MessageBoxButtons. OK, MessageBoxIcon. Warning).
return;
}
The finally
{
//close Excel application
If (objWorkbook!=null) objWorkbook. Close (System. Reflection. Missing. The Value, System. Reflection. Missing. The Value, System. Reflection. Missing. The Value).
If (objExcel Workbooks!=null) objExcel. Workbooks. The Close ();
If (objExcel!=null) objExcel. Quit ();

Objsheet=null;
ObjWorkbook=null;
ObjExcel=null;
}
MessageBox. Show (fileNameString + "export finished! ", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information);

I see on BBS adjust excel code is:
Worksheet. The Columns [1]. ColumnWidth=15;
Then combining the reality of himself, I'll change:
Objsheet. Columns [1]. ColumnWidth=15;
System error:
error 1 "this" method without the "1" the parameters of overloaded
Excuse me I should how to change in order to realize the function of adjusting excel column width? nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C #
  • Related