समस्या लेख में एक्सेल में डेटासेट से रूपांतरण के समाधान से कहीं आ रहा है। इस स्वयं के रूपांतरण का उपयोग करना एक अच्छा विचार नहीं है। Jet/ACE engine
या Microsoft Office Interop
का उपयोग करें। कम से कम वे गारंटी देते हैं, उनके पास ऐसी तरह की बग नहीं है, जो भविष्य में और अधिक हो सकती है। बेहतर कुछ ऐसा उपयोग करें जो समुदाय द्वारा पहले ही स्वीकार्य है। यहां मैंने एक दृष्टिकोण लिखा है कि इसे Interop
के साथ कैसे किया जाए।
सबसे पहले आपको क्या करना है Microsoft.Office.Interop.Excel
पर संदर्भ जोड़ने के लिए। यहां इसका तरीका करने के लिए है, जाहिर है msdn article
Add the Excel assembly as a reference to the project: Right-click on the project, select Add Reference.
Click the COM tab of the Add Reference dialog box, and find Microsoft Excel 11 Object Library.
Double-click on Microsoft Excel 11 Object Library, and press OK.
से लिया है, तो आप इसका इस्तेमाल एक्सेल 11 का बड़ा संस्करण है।
यहां कोड है, इसके वर्कफ़्लो के साथ टिप्पणियां/क्षेत्र हैं। आप संदर्भ के रूप में इस्तेमाल करना चाहिए using Excel = Microsoft.Office.Interop.Excel;
public void ExcelBtn_Click(object sender, EventArgs e)
{
DataSet dst = PrepareData();
byte[] bytes = ExportDataSetToExcel(dst);
Response.ClearContent();
Response.ContentType = "application/msoffice";
Response.AddHeader("Content-Disposition", @"attachment; filename=""ExportedExcel.xlsx"" ");
Response.BinaryWrite(bytes);
Response.End();
}
public static DataSet PrepareData()
{
DataTable badBoysDst = new DataTable("BadBoys");
badBoysDst.Columns.Add("Nr");
badBoysDst.Columns.Add("Name");
badBoysDst.Rows.Add(1, "Me");
badBoysDst.Rows.Add(2, "You");
badBoysDst.Rows.Add(3, "Pepe");
badBoysDst.Rows.Add(4, "Roni");
//Create a Department Table
DataTable goodBoysDst = new DataTable("GoodBoys");
goodBoysDst.Columns.Add("Nr");
goodBoysDst.Columns.Add("Name");
goodBoysDst.Rows.Add("1", "Not me");
goodBoysDst.Rows.Add("2", "Not you");
goodBoysDst.Rows.Add("3", "Quattro");
goodBoysDst.Rows.Add("4", "Stagioni");
DataTable goodBoysDst2 = new DataTable("GoodBoys2");
goodBoysDst2.Columns.Add("Nr");
goodBoysDst2.Columns.Add("Name");
goodBoysDst2.Rows.Add("1", "Not me");
goodBoysDst2.Rows.Add("2", "Not you");
goodBoysDst2.Rows.Add("3", "Quattro");
goodBoysDst2.Rows.Add("4", "Stagioni");
DataTable goodBoysDst3 = new DataTable("GoodBoys3");
goodBoysDst3.Columns.Add("Nr");
goodBoysDst3.Columns.Add("Name");
goodBoysDst3.Rows.Add("1", "Not me");
goodBoysDst3.Rows.Add("2", "Not you");
goodBoysDst3.Rows.Add("3", "Quattro");
goodBoysDst3.Rows.Add("4", "Stagioni");
//Create a DataSet with the existing DataTables
DataSet dst = new DataSet("SchoolBoys");
dst.Tables.Add(badBoysDst);
dst.Tables.Add(goodBoysDst);
dst.Tables.Add(goodBoysDst2);
dst.Tables.Add(goodBoysDst3);
return dst;
}
public static byte[] ExportDataSetToExcel(DataSet dst)
{
#region Create The Excel
Excel.Application excelApp = null;
Excel.Workbook excelWorkBook = null;
try
{
excelApp = new Excel.Application();
if (excelApp == null)
throw new Exception("You can throw custom exception here too");
excelWorkBook = excelApp.Workbooks.Add();
int sheetNr = 1;
foreach (DataTable table in dst.Tables)
{
Excel.Worksheet excelWorkSheet = null;
//Add a new worksheet or reuse first 3 sheets of workbook with the Datatable name
if (sheetNr <= excelWorkBook.Sheets.Count)
{
excelWorkSheet = excelWorkBook.Sheets.get_Item(sheetNr);
}
else
{
excelWorkSheet = excelWorkBook.Sheets.Add(After: excelWorkBook.Sheets[excelWorkBook.Sheets.Count]);
}
excelWorkSheet.Name = table.TableName;
for (int i = 1; i < table.Columns.Count + 1; i++)
{
excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
}
for (int j = 0; j < table.Rows.Count; j++)
{
for (int k = 0; k < table.Columns.Count; k++)
{
excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
}
}
sheetNr += 1;
}
//make first sheet active
excelApp.ActiveWorkbook.Sheets[1].Select();
excelWorkBook.SaveAs(@"c:\temp\DataSetToExcel.xlsx");
}
finally
{
excelWorkBook.Close();
excelApp.Quit();
//you should call GC here because there is memory problem with Interop
GC.Collect();
GC.WaitForPendingFinalizers();
}
#endregion
#region Take byte[] of the excel
byte[] result = null;
using (FileStream fs = new FileStream(@"c:\temp\DataSetToExcel.xlsx", FileMode.Open, FileAccess.Read))
{
BinaryReader reader = new BinaryReader(fs);
result = reader.ReadBytes((int)fs.Length);
}
#endregion
#region Delete the excel from the server
File.Delete(@"c:\temp\DataSetToExcel.xlsx");
#endregion
return result;
}
}
तो काफी पूर्ण उदाहरण कैसे Interop
साथ यह करने के लिए है कुछ समुदाय already.This द्वारा स्थापित किया गया उपयोग करने के लिए प्रयास करें। व्यक्तिगत रूप से मैं एसीई/जेट इंजन का उपयोग करना पसंद करता हूं, क्योंकि इंटरऑप में कोई मेमोरी लीक समस्या नहीं है (क्योंकि हम कोड में जीसी को कॉल कर रहे हैं)। एसीई/जेट इंजन के साथ नई चादरों का निर्माण थोड़ा कठिन है।
केस संवेदनशील स्ट्रिंग तुलना और ई-मेल पते का प्रारूप तालिकाओं के बीच समान नहीं है? – dlatikay
आपकी ToExcel() लाइब्रेरी 8 साल पहले से है। यदि आप मेरी (फ्री) सी # लाइब्रेरी का उपयोग करते हैं तो आप देखना चाहते हैं कि क्या होता है। इसमें कोड की केवल एक पंक्ति होती है (बस इसे अपना डेटासेट और फ़ाइल नाम पास करें) और यह ओपनएक्सएमएल पुस्तकालयों का उपयोग करके एक वास्तविक एक्सेल .xlsx फ़ाइल तैयार करेगा। http://mikesknowledgebase.com/pages/CSharp/ExportToExcel।एचटीएम –