Saturday 15 February 2014

c# - Gridview export to excel - Formatting problems -


I'm not sure what's going on here. I use this button event to export data from GridVideo to Excel and export the data, save the file, etc. etc., so it seems that I am working fine.

  private void button 2_Click (object sender, Iventargs e) {Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application (); Microsoft.Office.Interop.Excel._Workbook Workbook = app.Workbooks.Add (Type.Missing); Microsoft.Office.Interop.Excel._Worksheet Worksheet = Faucet; App.Visible = true; {Workheet = (Microsoft.Office.Interop.Excel.Worksheet) Try the workbook. Sheet ["sheet 1"]; Worksheet = (Microsoft.Office.Interop.Excel.Worksheet) Workbook. active sheet; Worksheet Name = "geoetta environmental data"; (Int i = 1; i & lt; dataGridView1.Column.Count + 1; for i ++) (worksheet. Seals [1, i] = Detagrid scene 1st column [i-1] .HeaderText; for} (int i = 0; i & lt; dataGridView1.Rows.Count - 1; i ++) {for (int j = 0; j & lt; dataGridView1.Column.Count; j ++) {Caryptrkkkaljh [i + 2, j + 1] = Detagrid scene 1. Rao [i] Ksel [J]. value. Tostring ();}} string fileName = String.Empty; SaveFileDialog saveFileDialog1 = new SaveFileDialog (); SaveFileDialog1.filter = " Excel files | * .xls | all files (*. *) | *. * "; SaveFileDialog1.FilterIndex = 2; SaveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog () == DialogResult.OK) {fileName = saveFileDialog1. FileName; Workbook Ika. Sevas (filename, type. Masing, type. Missing type. Missing type. Missing type. Missing, Maikrosoftkofiskintrop.akssel. Aksselsveasaksaksmoks.aksaksaks type. Masing, type. Missing type. Missing, Type missing, type missing);} and return;} catch (System.Exception ex) {} finally {app.Quit}; Workbook = null; App = null; }}   

The best side of the problem I am facing is basically even forming export values ​​as numbers, I can not use them. I can not even add them! If I manually rewrite the value in the same cell then it becomes useful whether it is anything with the export process?

In this way I load the data in the Datagram view:

  var time = DateTime.Now.ToString ("HH: mm: ss"); DataGridView1.Rows.Add (new string [] {time, text box 1. text, text box 2. text});   

Enter image details here

The problem arises from this line and how the datagroup view population is:

  worksheet.Cells [i 2 , J 1] = Datagrid view 1. Rao [i]. Sel [J]. Value. Toasting ();   

The data is exported as string .toString () , whatever the string it is, the string is there.

As DataGridViewCell.Value has a string , so before you export dgvCell in an numerical value. Value must be entered. For example:

  if (j == 1 || j == 2) {worksheet.Cells (i 2, j1) = convert toDecimal (dataGridView1.Rows (i). ( J). Value)} other {worksheet. Seals [i2, j 1] = datagrid view 1. Ro [i] .cell [j]. Value. Toasting (); }   

If there is numerical value (decimal, double, integer, ...) in DataGridViewCell.Value, simply remove .toString () and it should work as expected (Excel will set the appropriate type)

In both cases, if you want to apply a custom format to appear in Excel , You will lose it during the export, and you must explicitly set it in Excel to use it in the property . This Q / A can help you to get it:



No comments:

Post a Comment