2011-06-01 15 views
6

हे दोस्तों मैं एक्सेल करने के लिए डेटाग्रिडव्यू के डेटा निर्यात करने में सक्षम था। लेकिन डेटाग्रिडव्यू का वास्तविक प्रारूप निर्यात नहीं किया गया था, जैसे फ़ॉन्ट, रंग और स्थान। तो, क्या डेटा के लिए डेटाग्रिडव्यू को निर्यात करने का कोई सबसे अच्छा तरीका है यानी न केवल डेटा बल्कि देखो।डेटाग्रिडव्यू निर्यात उत्कृष्टता

नमूना नज़र है: - enter image description here

+0

ऐसा लगता है कि आप किसी प्रकार की रिपोर्ट प्रदर्शित कर रहे हैं। मैं सुझाव दूंगा कि आप क्रिस्टल रिपोर्ट्स या एसएसआरएस का उपयोग करें। उन्होंने अन्य प्रारूपों में निर्यात करने के लिए सुविधाओं में बनाया है। यदि आप मैन्युअल रूप से निर्यात करते हैं तो आपको हर बार रिपोर्ट रिपोर्ट बदलने पर निर्यात फ़ंक्शन को संशोधित करना होगा। – Eranga

उत्तर

5

CSV का निर्यात प्रयास करें

private void ToCsV(DataGridView dGV, string filename) 
    { 
     string separator = ","; 
     StringBuilder stOutput = new StringBuilder(); 
     // Export titles: 
     StringBuilder sHeaders = new StringBuilder(); 
     for (int j = 0; j < dGV.Columns.Count; j++) 
     { 
      sHeaders.Append(dGV.Columns[j].HeaderText); 
      sHeaders.Append(separator); 
     } 
     stOutput.AppendLine(sHeaders.ToString()); 
     // Export data. 
     for (int i = 0; i < dGV.RowCount - 1; i++) 
     { 
      StringBuilder stLine = new StringBuilder(); 
      for (int j = 0; j < dGV.ColumnCount; j++) 
      { 
       stLine.Append(Convert.ToString(dGV[j, i].Value)); 
       stLine.Append(separator); 
      } 
      stOutput.AppendLine(stLine.ToString()); 
     } 

     File.WriteAllText(filename, stOutput.ToString()); 
    } 
+0

मुझे 'System.Windows.Forms.DataGridViewRowCollection' के रूप में एक त्रुटि मिल रही है 'सेल' \t 'की परिभाषा नहीं है' क्या आप मेरी मदद कर सकते हैं – Dotnet

+0

इसमें इसमें बहुत सारी त्रुटियां हैं! dGV.Rows.Cells dGV.Rows होना चाहिए [i] .ल्स और के लिए (int i = 0; i

0

इससे पहले कि आप button_Click घटना में कोड लिखते हैं, आप Microsoft.Office.Interop.Excel वस्तु पुस्तकालय के लिए एक संदर्भ जोड़ना होगा।

अपनी परियोजना पर राइट क्लिक करें और संदर्भ मेनू जोड़ें का चयन करें। उसके बाद .NET टैब पर जाएं और चुनें और Microsoft.Office.Interop.Excel जोड़ें।

बटन_Click ईवेंट में नीचे दिए गए कोड को लिखें।

  // button_Click event 

      private void button11_Click(object sender, EventArgs e) 
      { 
       // creating Excel Application 
       string fileName = String.Empty; 
       Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
       // creating new WorkBook within Excel application 
       Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
       // creating new Excelsheet in workbook 
       Microsoft.Office.Interop.Excel._Worksheet worksheet = null; 
       // see the excel sheet behind the program 
       app.Visible = true; 
       // get the reference of first sheet. By default its name is Sheet1. 
       // store its reference to worksheet 
       try 
       { 
        //Fixed:(Microsoft.Office.Interop.Excel.Worksheet) 
        worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; 
        worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; 
        // changing the name of active sheet 
        worksheet.Name = "Exported from AMIT"; 
        // storing header part in Excel 
        for (int i = 1; i < dataGridView1.Columns.Count + 1; i++) 
        { 
          worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText; 
        } 
        // storing Each row and column value to excel sheet 
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 
        { 
          for (int j = 0; j < dataGridView1.Columns.Count; j++) 
          { 
           worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); 
          } 
        } 


        // Save The Application 
        SaveFileDialog saveFileExcel = new SaveFileDialog(); 

        saveFileExcel.Filter = "Excel files |*.xls|All files (*.*)|*.*"; 
        saveFileExcel.FilterIndex = 2; 
        saveFileExcel.RestoreDirectory = true; 


        if (saveFileExcel.ShowDialog() == DialogResult.OK) 
        { 
          fileName = saveFileExcel.FileName; 
          //Fixed-old code :11 para->add 1:Type.Missing 
          workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

        } 
        else 
        { 
          return; 

          // Exit from the application 
          //app.Quit(); 
        } 
       } 
       catch (Exception) 
       { 
        //Statement; 
       } 
       finally 
       { 
        app.Quit(); 
        workbook = null; 
        app = null; 
       } 
      } 
संबंधित मुद्दे