2012-11-26 13 views
5

मैं एक्सेल में DataGridView डेटा की नकल करने की कोशिश कर रहा हूँ और मैं इस कोड का उपयोग कर रहा करने के लिए DataGridView निर्यात करने के लिए:सरल तरीका एक्सेल

public static void ExportToExcel(DataGridView dgView) 
{ 
    Microsoft.Office.Interop.Excel.Application excelApp = null; 

    try 
    { 
     // instantiating the excel application class 
     object misValue = System.Reflection.Missing.Value; 
     excelApp = new Microsoft.Office.Interop.Excel.Application(); 
     Microsoft.Office.Interop.Excel.Workbook currentWorkbook = excelApp.Workbooks.Add(Type.Missing); 
     Microsoft.Office.Interop.Excel.Worksheet currentWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)currentWorkbook.ActiveSheet; 

     currentWorksheet.Columns.ColumnWidth = 18; 

     if (dgView.Rows.Count > 0) 
     { 
      currentWorksheet.Cells[1, 1] = DateTime.Now.ToString("s"); 
      int i = 1; 

      foreach (DataGridViewColumn dgviewColumn in dgView.Columns) 
      { 
       // Excel work sheet indexing starts with 1 
       currentWorksheet.Cells[2, i] = dgviewColumn.Name; 
       ++i; 
      } 

      Microsoft.Office.Interop.Excel.Range headerColumnRange = currentWorksheet.get_Range("A2", "G2"); 
      headerColumnRange.Font.Bold = true; 
      headerColumnRange.Font.Color = 0xFF0000; 

      //headerColumnRange.EntireColumn.AutoFit(); 
      int rowIndex = 0; 

      for (rowIndex = 0; rowIndex < dgView.Rows.Count; rowIndex++) 
      { 
       DataGridViewRow dgRow = dgView.Rows[rowIndex]; 

       for (int cellIndex = 0; cellIndex < dgRow.Cells.Count; cellIndex++) 
       { 
        currentWorksheet.Cells[rowIndex + 3, cellIndex + 1] = dgRow.Cells[cellIndex].Value; 
       } 
      } 

      Microsoft.Office.Interop.Excel.Range fullTextRange = currentWorksheet.get_Range("A1", "G" + (rowIndex + 1).ToString()); 
      fullTextRange.WrapText = true; 
      fullTextRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; 
     } 
     else 
     { 
      string timeStamp = DateTime.Now.ToString("s"); 
      timeStamp = timeStamp.Replace(':', '-'); 
      timeStamp = timeStamp.Replace("T", "__"); 
      currentWorksheet.Cells[1, 1] = timeStamp; 
      currentWorksheet.Cells[1, 2] = "No error occured"; 
     } 

     using (SaveFileDialog exportSaveFileDialog = new SaveFileDialog()) 
     { 
      exportSaveFileDialog.Title = "Select Excel File"; 
      exportSaveFileDialog.Filter = "Microsoft Office Excel Workbook(*.xlsx)|*.xlsx"; 

      if (DialogResult.OK == exportSaveFileDialog.ShowDialog()) 
      { 
       string fullFileName = exportSaveFileDialog.FileName; 
       // currentWorkbook.SaveCopyAs(fullFileName); 
       // indicating that we already saved the workbook, otherwise call to Quit() will pop up 
       // the save file dialogue box 

       currentWorkbook.SaveAs(fullFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, System.Reflection.Missing.Value, misValue, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, true, misValue, misValue, misValue); 
       currentWorkbook.Saved = true; 
       MessageBox.Show("Exported successfully", "Exported to Excel", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
    finally 
    { 
     if (excelApp != null) 
     { 
      excelApp.Quit(); 
     } 
    } 
} 

लेकिन वहाँ के रूप में 200 000 रिकॉर्ड खत्म हो गया है, इसकी एक लंबे समय के निर्यात करने के लिए ले जा रहा। क्या ऐसा करने का कोई तेज तरीका है?

+0

ये लिंक आपकी मदद कर सकते हैं: [link1] (http://stackoverflow.com/a/11742908/1135581) और [link2] (http://www.codeproject.com/Articles/19284/ निर्यात-बड़े-डेटा-से-ग्रिडव्यू-एंड-डेटारेडर-टू) – Rahul

उत्तर

7

इस कोड को आज़माएं। यह सामान्य इंटरऑप विधियों की तुलना में तेज़ है, यह सीएसवी में परिवर्तित हो जाता है जिसे एक्सेल द्वारा आसानी से पढ़ा जा सकता है।

int cols; 
//open file 
StreamWriter wr = new StreamWriter("GB STOCK.csv"); 

//determine the number of columns and write columns to file 
cols = dgvStock.Columns.Count; 
for (int i = 0; i < cols - 1; i++) 
{ 
    wr.Write(dgvStock.Columns[i].Name.ToString().ToUpper() + ","); 
} 
wr.WriteLine(); 

//write rows to excel file 
for (int i = 0; i < (dgvStock.Rows.Count - 1); i++) 
{ 
    for (int j = 0; j < cols; j++) 
    { 
     if (dgvStock.Rows[i].Cells[j].Value != null) 
     { 
      wr.Write(dgvStock.Rows[i].Cells[j].Value + ","); 
     } 
     else 
     { 
      wr.Write(","); 
     } 
    } 

    wr.WriteLine(); 
} 

//close file 
wr.Close(); 
+0

कोड प्रत्येक पंक्ति को 1 एक्सेल सेल (एमएसवीएस 2005, .net2) में रखता है। – Vadim

-2

सी # में एएसपीनेट में एक्सेल में एक्सेल करने के लिए ग्रिड व्यू डेटा को कैसे निर्यात करें। अधिक जानकारी click here

+3

ध्यान दें कि [लिंक-केवल उत्तर] (http://meta.stackoverflow.com/tags/link-only-answers/info) निराश हैं, SO उत्तर समाधान के लिए खोज का अंत बिंदु होना चाहिए (बनाम। फिर भी संदर्भों का एक और स्टॉपओवर, जो समय के साथ पुराना हो जाता है)। लिंक को संदर्भ के रूप में रखते हुए, यहां स्टैंड-अलोन सारांश जोड़ना पर विचार करें। – kleopatra

+0

हैलो सर इस लिंक को चेक करें, यह उस प्रश्न का उत्तर है – Jquery

+0

हैलो महोदया, मेरी आखिरी टिप्पणी में लिंक पढ़ें :-) – kleopatra