تصدير البيانات إلى Excel

في هذه المقالة ، سأوضح لك كيفية تصدير البيانات إلى Excel في C # و VB.NET من مصادر بيانات مختلفة مثل مجموعة ، مجموعة من الكائنات المخصصة ، DataTable ، DataView ، DataGrid ، GridView ، HTML ، JSON و CSV.

تصدير البيانات إلى Excel في C # باستخدام Aspose.Cells API

Aspose.Cells for .NET هي واجهة برمجة تطبيقات قوية لمعالجة جداول البيانات تتيح لك إنشاء ملفات Excel أو تحريرها أو تحويلها ضمن تطبيقات .NET. تمكّنك طرق واجهة برمجة التطبيقات سهلة الاستخدام من أداء ميزات أتمتة Excel بسلاسة في بضعة أسطر من التعليمات البرمجية. NuGet هو أسهل طريقة لتنزيل وتثبيت Aspose.Cells API for .NET. افتح نافذة إدارة حزم NuGet واكتب “Aspose.Cells” في مربع نص البحث للعثور على حزمة Aspose.Cells .NET. أخيرًا ، انقر فوق الزر “تثبيت” لتثبيت أحدث إصدار من الحزمة.

تصدير صفيف إلى Excel في C

يمكننا تصدير مصفوفة (أحادية أو ثنائية الأبعاد) من نوع مرجعي أو نوع قيمة إلى مستند Excel. نستخدم طريقة ImportArray لمجموعة الخلايا لتصدير البيانات إلى جدول بيانات من مصفوفة. الإصدارات المحملة بشكل زائد من أسلوب ImportArray هي كالتالي.

الاسمالوصف
[ImportArray (Double []، Int32، Int32، Boolean)] (https://apireference.aspose.com/cells/net/aspose.cells.cells/importarray/methods/1)يصدر مصفوفة مزدوجة إلى ورقة عمل.
[ImportArray (Int32 []، Int32، Int32، Boolean)] (https://apireference.aspose.com/cells/net/aspose.cells.cells/importarray/methods/3)يصدر مصفوفة من عدد صحيح إلى ورقة عمل.
[ImportArray (String []، Int32، Int32، Boolean)] (https://apireference.aspose.com/cells/net/aspose.cells.cells/importarray/methods/5)يصدر مصفوفة من السلسلة إلى ورقة عمل.
[ImportArray (Double [،]، Int32، Int32)] (https://apireference.aspose.com/cells/net/aspose.cells/cells/methods/importarray/)يصدر مصفوفة ثنائية الأبعاد للمضاعفة إلى ورقة عمل.
[ImportArray (Int32 [،]، Int32، Int32)] (https://apireference.aspose.com/cells/net/aspose.cells.cells/importarray/methods/2)يصدر مصفوفة ثنائية الأبعاد لعدد صحيح إلى ورقة عمل.
[ImportArray (String [،]، Int32، Int32)] (https://apireference.aspose.com/cells/net/aspose.cells.cells/importarray/methods/4)يصدر مصفوفة سلسلة ثنائية الأبعاد إلى ورقة عمل.

يأخذ الحمل الزائد النموذجي المعلمات التالية:

  • Array ، كائن المصفوفة الذي تقوم بتصدير المحتوى منه.
  • رقم الصف ، رقم صف الخلية الأولى (المستندة إلى الصفر) التي سيتم تصدير البيانات إليها.
  • رقم العمود ، رقم العمود الخاص بالخلية الأولى (القائمة على الصفر) التي سيتم تصدير البيانات إليها.
  • عمودي ، قيمة منطقية تحدد ما إذا كان سيتم تصدير البيانات عموديًا أو أفقيًا.

فيما يلي خطوات تصدير مصفوفة إلى ملف Excel في C #.

  • قم بإنشاء كائن مصنف. فئة مصنف تمثل ملف Microsoft Excel.
  • احصل على مرجع إلى ورقة العمل المطلوبة. تحتوي فئة Workbook على مجموعة Worksheets التي تتيح الوصول إلى كل ورقة عمل في ملف Excel.
  • قم باستدعاء طريقة ImportArray لمجموعة خلايا لتصدير مصفوفة إلى ورقة العمل في الصف والعمود المحددين. توفر فئة ورقة العمل مجموعة خلايا.
  • احفظ ملف Excel باستخدام طريقة Workbook.Save (سلسلة).

يوضح نموذج التعليمات البرمجية التالي كيفية تصدير صفيف سلسلة إلى ملف Excel في C #.

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];

// Creating an array containing names as string values
string[] names = new string[] { "Laurence Chen", "Roman Korchagin", "Kyle Huang" };

// Exporting the array of names to first row and first column vertically
worksheet.Cells.ImportArray(names, 0, 0, true);

// Saving the Excel file
workbook.Save("StringsArray.xlsx");
تصدير صفيف السلسلة إلى Excel

تصدير مجموعة من البيانات إلى Excel

وبالمثل ، يمكننا تصدير مصفوفة ثنائية الأبعاد إلى ملف Excel. يوضح نموذج التعليمات البرمجية التالي كيفية تصدير صفيف ثنائي الأبعاد إلى ملف Excel في C #.

// Creating a two-dimensional array of integers
int[,] array2D = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

// Exporting a two-dimensional array at the first row and first column of the worksheet
worksheet.Cells.ImportArray(array2D, 0, 0);

تصدير ArrayList إلى Excel في C

لتصدير البيانات من ArrayList إلى ورقة العمل ، قم باستدعاء أسلوب Cells للمجموعة ImportArrayList. تأخذ طريقة ImportArrayList المعلمات التالية:

  • قائمة الصفيف تمثل كائن ArrayList الذي تقوم بتصديره.
  • يمثل رقم الصف رقم صف الخلية الأولى التي سيتم تصدير البيانات إليها.
  • يمثل رقم العمود رقم العمود الخاص بالخلية الأولى التي سيتم تصدير البيانات إليها.
  • هي قيمة رأسية منطقية تحدد ما إذا كان سيتم تصدير البيانات عموديًا أو أفقيًا.

يُظهر نموذج التعليمات البرمجية التالي كيفية تصدير ArrayList إلى ملف Excel في C #.

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];

// Instantiating an ArrayList object
ArrayList list = new ArrayList();

// Add few names to the list as string values
list.Add("Laurence Chen");
list.Add("Roman Korchagin");
list.Add("Kyle Huang");
list.Add("Tommy Wang");

// Exporting the contents of ArrayList vertically at the first row and first column of the worksheet. 
worksheet.Cells.ImportArrayList(list, 0, 0, true);

// Saving the Excel file
workbook.Save("ArrayListExport.xlsx");

تصدير مجموعة من الكائنات المخصصة إلى Excel في C

لتصدير البيانات من مجموعة كائنات مخصصة إلى ورقة عمل ، نستخدم طريقة ImportCustomObjects. هناك نوعان من الإصدارات المحملة بشكل زائد من هذه الطريقة.

  1. ImportCustomObjects (قائمة ICollection ، String [] propertyNames ، Boolean isPropertyNameShown ، Int32 firstRow ، Int32 firstColumn ، Int32 rowNumber ، Boolean insertions ، String dateFormatString ، Boolean convertStringToNumber)
  2. ImportCustomObjects (قائمة ICollection ، Int32 firstRow ، Int32 firstColumn ، خيارات ImportTableOptions)

سوف نستكشف كل طريقة محملة فوق طاقتها واحدة تلو الأخرى. فيما يلي وصف لمعلمات الطريقة الأولى المحملة بشكل زائد:

  • list هي مجموعة الكائنات المخصصة.
  • propertyNames أسماء خصائص الكائن المراد تصديره. إذا كانت خالية ، فسيتم تصدير جميع الخصائص.
  • isPropertyNameShown يشير إلى ما إذا كان سيتم تصدير أسماء الخصائص إلى الصف الأول.
  • firstRow رقم صف الخلية الأولى المطلوب التصدير إليها.
  • firstColumn رقم عمود الخلية الأولى المطلوب التصدير إليها.
  • rowNumber عدد العناصر التي سيتم تصديرها.
  • insertRows يشير إلى ما إذا كانت الصفوف الإضافية ستتم إضافتها لملاءمة البيانات.
  • dateFormatString سلسلة تنسيق التاريخ للخلايا.
  • convertStringToNumber يشير إلى ما إذا كانت هذه الطريقة ستحاول تحويل سلسلة إلى رقم.

في المثال التالي ، نقوم بتصدير قائمة كائنات الشخص إلى مستند Excel في C #. يرجى ملاحظة أننا نقوم بتصدير خاصيتين فقط (الاسم والعمر) لكائن الشخص.

// Instantiate a new Workbook
Workbook book = new Workbook();
// Obtaining the reference of the worksheet
Worksheet sheet = book.Worksheets[0];

// Define List
List<Person> list = new List<Person>();

list.Add(new Person("Mike", 25, "Software Engineer"));
list.Add(new Person("Steve", 30, "Doctor"));
list.Add(new Person("Billy", 35, "Teacher"));

// We pick only Name and Age columns, not all, to export to the worksheet         
sheet.Cells.ImportCustomObjects((System.Collections.ICollection)list,
    new string[] { "Name", "Age" }, // propertyNames
    true, // isPropertyNameShown
    0, // firstRow
    0, // firstColumn
    list.Count, // Number of objects to be exported
    true, // insertRows
    null, // dateFormatString
    false); // convertStringToNumber

// Save the Excel file
book.Save("ExportedCustomObjects.xlsx");
       
public class Person
{
    public string Name { get; set; }

    public int Age { get; set; }

    public string Occupation { get; set; }

    public Person(string name, int age, string occupation)
    {
        Age = age;
        Name = name;
        Occupation = occupation;
    }
}
تصدير قائمة الكائنات إلى Excel

تصدير قائمة كائنات الأشخاص إلى Excel

الآن نستكشف الطريقة الثانية المحملة بشكل زائد من ImportCustomObjects. فيما يلي وصف لعوامل الطريقة:

  • قائمة قائمة الكائنات المخصصة.
  • firstRow رقم صف الخلية الأولى المطلوب التصدير إليها.
  • firstColumn رقم عمود الخلية الأولى المطلوب التصدير إليها.
  • خيارات ImportTableOptions كائن.

توفر المعلمة ImportTableOptions عدة خيارات لتصدير البيانات إلى الخلايا. وفيما يلي بعض منها:

  • CheckMergedCells هل يحتوي مستند Excel على خلايا مدمجة.
  • مصفوفة عدد صحيح من فهارس العمود ColumnIndexes (المستندة إلى 0) للتصدير من مصدر البيانات. فارغ يعني أنه يجب تصدير جميع الأعمدة.
  • ConvertGridStyle يشير إلى ما إذا كان سيتم تطبيق نمط عرض الشبكة على الخلايا.
  • ConvertNumericData قيمة منطقية تشير إلى ما إذا كان يجب تحويل قيمة السلسلة إلى قيمة رقمية أو قيمة تاريخ.
  • DateFormat الحصول على سلسلة تنسيق التاريخ أو تعيينها للخلايا ذات قيم التاريخ والوقت المصدرة.
  • القيم الافتراضية القيمة الافتراضية للخلية في الجدول خالية.
  • InsertRows يشير إلى ما إذا كان يجب إضافة صفوف جديدة لتصدير سجلات البيانات.
  • IsFieldNameShown يشير إلى ما إذا كان يجب تصدير أسماء الحقول.
  • يشير IsFormulas إلى ما إذا كانت البيانات عبارة عن صيغ.
  • IsHtmlString يشير إلى ما إذا كانت البيانات تحتوي على علامات HTML. إذا قمنا بتعيين القيمة على true ، فسيظل تنسيق HTML محفوظًا أثناء تصدير البيانات إلى مستند Excel.
  • NumberFormats الحصول على أو تعيين تنسيقات الأرقام
  • يشير ShiftFirstRowDown إلى ما إذا كان يجب نقل الصف الأول إلى أسفل عند إدراج الصفوف.
  • TotalColumns الحصول على أو تعيين إجمالي عدد الأعمدة للتصدير من مصدر بيانات. -1 تعني جميع أعمدة مصدر البيانات المحدد.
  • TotalRows الحصول على أو تعيين إجمالي عدد الصفوف للتصدير من مصدر البيانات. -1 تعني جميع صفوف مصدر البيانات المحدد.

في المثال التالي ، نقوم بتصدير البيانات من مجموعة كائنات إلى ورقة عمل تحتوي على خلايا مدمجة. نقوم بتعيين قيمة الخاصية ImportTableOptions.CheckMergedCells على true لأن مستند Excel يحتوي على خلايا مدمجة.

// Opening an existing Workbook.
Workbook workbook = new Workbook("SampleMergedTemplate.xlsx");
List<Product> productList = new List<Product>();

// Creating a collection of Products
for (int i = 0; i < 3; i++)
{
    Product product = new Product
    {
        ProductId = i,
        ProductName = "Test Product - " + i
    };
    productList.Add(product);
}

ImportTableOptions tableOptions = new ImportTableOptions();
// Set CheckMergedCells property to true
tableOptions.CheckMergedCells = true;
tableOptions.IsFieldNameShown = false;

//Export data to excel template (in second row, first column) 
workbook.Worksheets[0].Cells.ImportCustomObjects((ICollection)productList, 1, 0, tableOptions);
workbook.Save("SampleMergedTemplate_out.xlsx", SaveFormat.Xlsx);

public class Product
{
    public int ProductId { get; set; }

    public string ProductName { get; set; }
}
تصدير البيانات من مجموعة كائنات إلى ورقة عمل تحتوي على خلايا مدمجة

تصدير البيانات إلى مستند Excel يحتوي على خلايا مدمجة

ينسخ الصفوف والأعمدة من ملف Excel إلى ملف آخر في C

يمكننا نسخ الصفوف والأعمدة برمجيًا من مستند Excel إلى آخر. عند نسخ صف (أو عمود) ، يتم أيضًا نسخ البيانات الموجودة فيه ، بما في ذلك الصيغ - مع المراجع المحدثة - والقيم والتعليقات والتنسيقات والخلايا المخفية والصور والكائنات الرسومية الأخرى. يمكننا أيضًا نسخ الصفوف والأعمدة في نفس ورقة العمل أو عبر أوراق عمل مختلفة في مستند Excel. يوفر Aspose.Cells الطرق التالية لنسخ الصفوف والأعمدة.

يوضح رمز المثال التالي كيفية نسخ الصفوف من مستند Excel إلى مستند آخر في C #.

// Open the source excel file.
Workbook srcWorkbook = new Workbook("Source_Workbook.xlsx");

// Instantiate the destination excel file.
Workbook destWorkbook = new Workbook();

// Get the first worksheet of the source workbook.
Worksheet srcWorksheet = srcWorkbook.Worksheets[0];

// Get the first worksheet of the destination workbook.
Worksheet desWorksheet = destWorkbook.Worksheets[0];

// Copy all the rows of the first worksheet of source Workbook to
// the first worksheet of destination Workbook.
desWorksheet.Cells.CopyRows(srcWorksheet.Cells, 0, 0, srcWorksheet.Cells.MaxDisplayRange.RowCount);

// Save the excel file.
destWorkbook.Save("Destination_Workbook.xlsx");

يوضح رمز المثال التالي كيفية نسخ صفوف معينة من مستند Excel إلى مستند آخر.

// Open the source excel file.
Workbook srcWorkbook = new Workbook("Source_Workbook.xlsx");

// Instantiate the destination excel file.
Workbook destWorkbook = new Workbook();

// Get the first worksheet of the source workbook.
Worksheet srcWorksheet = srcWorkbook.Worksheets[0];

// Get the first worksheet of the destination workbook.
Worksheet desWorksheet = destWorkbook.Worksheets[0];

// Copy the second row of the source Workbook to the first row of destination Workbook.
desWorksheet.Cells.CopyRow(srcWorksheet.Cells, 1, 0);

// Copy the fourth row of the source Workbook to the second row of destination Workbook.
desWorksheet.Cells.CopyRow(srcWorksheet.Cells, 3, 1);

// Save the excel file.
destWorkbook.Save("Destination_Workbook.xlsx");
ينسخ بيانات الصفوف من مستند Excel إلى آخر

يمكننا بالمثل نسخ بيانات الأعمدة من مستند Microsoft Excel إلى مستند آخر باستخدام طريقة CopyColumn أو CopyColumns.

تصدير DataTable إلى Excel في C

يمكن تصدير البيانات من كائنات ADO.NET مثل DataTable و DataColumn و DataView إلى أوراق عمل Excel. لتصدير البيانات من DataTable ، نسمي طريقة ImportData لتجميع الخلايا. هناك العديد من الإصدارات المحملة بشكل زائد من طريقة ImportData ولكننا نستخدم ما يلي:

public int ImportData(
	DataTable table,
	int firstRow,
	int firstColumn,
	ImportTableOptions options
)

وصف المعلمات موضح أدناه:

في نموذج التعليمات البرمجية التالي ، نقوم بإنشاء كائن DataTable يحتوي على ثلاثة أعمدة وصفين. وتصديره إلى ورقة عمل Excel.

// Instantiating a Workbook object            
Workbook workbook = new Workbook();

// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];

// Instantiating a "Products" DataTable object
DataTable dataTable = new DataTable("Products");

// Adding columns to the DataTable object
dataTable.Columns.Add("Product ID", typeof(int));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(int));

// Creating an empty row in the DataTable object
DataRow dr = dataTable.NewRow();

// Adding data to the row
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;

// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

// Creating another empty row in the DataTable object
dr = dataTable.NewRow();

// Adding data to the row
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;

// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

// Setting IsFieldNameShown property to true will add column names // of the DataTable to the worksheet as a header row
ImportTableOptions tableOptions = new ImportTableOptions();
tableOptions.IsFieldNameShown = true;

// Exporting the contents of DataTable at the first row and first column.
worksheet.Cells.ImportData(dataTable, 0, 0, tableOptions);

// Saving the Excel file
workbook.Save("DataTable_Eport.xlsx");
تصدير DataTable إلى Excel

تصدير DataTable إلى Excel

تصدير بيانات أعمدة البيانات الانتقائية إلى Excel في C

يمكننا تصدير أعمدة DataColumns الانتقائية من DataTable أو DataView إلى مستند Excel. كما تمت مناقشته سابقًا ، يقبل الأسلوب ImportData وسيطة من النوع ImportTableOptions. تحتوي الفئة ImportTableOptions على خاصية ColumnIndexes والتي تقبل مصفوفة من فهارس الأعمدة (المستندة إلى الصفر) التي نريد تصديرها. في نموذج التعليمات البرمجية التالي ، نقوم بتصدير عمودين فقط من DataColumns من DataTable إلى ورقة عمل Excel.

// Instantiating a "Products" DataTable object
DataTable dataTable = new DataTable("Products");

// Adding columns to the DataTable object
dataTable.Columns.Add("Product ID", typeof(int));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(int));

// Creating an empty row in the DataTable object
DataRow dr = dataTable.NewRow();

// Adding data to the row
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;

// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

// Creating another empty row in the DataTable object
dr = dataTable.NewRow();

// Adding data to the row
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;

// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

// Instantiate a new Workbook
Workbook book = new Workbook();

Worksheet sheet = book.Worksheets[0];

// Create export options
ImportTableOptions importOptions = new ImportTableOptions();
// Sets the columns (0-based) to export from data source.
// null means all columns should be exported.
importOptions.ColumnIndexes = new int[] { 0, 1 };
importOptions.IsFieldNameShown = true;

// Exporting the values of 1st and 2nd columns of the data table
sheet.Cells.ImportData(dataTable, 0, 0, importOptions);

book.Save("DataColumsExport.xlsx");
تصدير بيانات أعمدة البيانات الانتقائية إلى Excel

إخراج DataColumns إلى Excel

تصدير البيانات من DataView إلى Excel في C

A DataView هو عرض على DataTable يمكن تخصيصه لتقديم مجموعة فرعية من البيانات من DataTable. نستخدم الإصدار الزائد التالي من طريقة ImportData لتصدير البيانات من DataView إلى مستند Excel.

public int ImportData(
	DataView dataView,
	int firstRow,
	int firstColumn,
	ImportTableOptions options
)

نعلم أن هناك طريقتان لإنشاء DataView. يمكننا استخدام مُنشئ DataView ، أو يمكننا إنشاء مرجع إلى خاصية DefaultView لخاصية DataTable. في نموذج التعليمات البرمجية التالي ، نستخدم الطريقة الأحدث لإنشاء DataView.

worksheet.Cells.ImportData(dataTable.DefaultView, 0, 0, options);

تصدير البيانات من DataGrid و GridView إلى Excel في C

Aspose.Cells مكتبة تسمح لنا بتصدير البيانات من عناصر تحكم Microsoft Grid مثل DataGrid و GridView إلى ورقة عمل Excel. يوفر طريقة ImportDataGrid ، لتصدير البيانات من DataGrid و ImportGridView طريقة لتصدير البيانات من GridView.

هناك العديد من الإصدارات المحملة بشكل زائد من أسلوب ImportDataGrid ولكن الحمل الزائد النموذجي يأخذ المعلمات التالية:

  • dataGrid ، وهو كائن DataGrid الذي نقوم بتصدير المحتوى منه.
  • firstRow ، رقم صف الخلية الأولى التي سيتم تصدير البيانات إليها.
  • firstColumn ، رقم العمود الخاص بالخلية الأولى التي سيتم تصدير البيانات إليها.
  • insertRows ، خاصية منطقية تشير إلى ما إذا كان يجب إضافة صفوف إضافية إلى ورقة العمل لاحتواء البيانات.
  • importStyle ، خاصية منطقية تشير إلى ما إذا كان يجب تصدير نمط الخلية.

يوضح مثال التعليمات البرمجية التالي كيفية تصدير البيانات من DataGrid إلى ملف Excel في C #.

// Create a DataTable object and set it as the DataSource of the DataGrid.
DataTable dataTable = new DataTable("Products");
dataTable.Columns.Add("Product ID", typeof(int));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(int));

DataRow dr = dataTable.NewRow();
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;
dataTable.Rows.Add(dr);

dr = dataTable.NewRow();
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;
dataTable.Rows.Add(dr);

// Now take care of DataGrid
DataGrid dg = new DataGrid();
dg.DataSource = dataTable;
dg.DataBind();

// We have a DataGrid object with some data in it.
// Lets export it to an Excel worksheet.

// Creat a new workbook
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

// Exporting the contents of the DataGrid to the worksheet
worksheet.Cells.ImportDataGrid(dg, 0, 0, false);

// Save it as Excel file
workbook.Save("ExportDataGrid.xlsx");

تصدير بيانات بتنسيق HTML إلى Excel في C

Aspose.Cells يتيح لك تصدير بيانات بتنسيق HTML إلى ورقة عمل Excel. تحلل واجهة برمجة التطبيقات نصًا بتنسيق HTML أثناء تصدير البيانات وتحويل HTML إلى قيم خلية منسقة. في نموذج التعليمات البرمجية التالي ، يحتوي DataTable على نص بتنسيق HTML ونقوم بتصديره إلى مستند Excel باستخدام طريقة ImportData.

// Prepare a DataTable with some HTML formatted values
DataTable dataTable = new DataTable("Products");

dataTable.Columns.Add("Product ID", typeof(int));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(int));

DataRow dr = dataTable.NewRow();
dr[0] = 1;
// Make text italicize
dr[1] = "<i>Aniseed</i> Syrup";
dr[2] = 15;
dataTable.Rows.Add(dr);

dr = dataTable.NewRow();
dr[0] = 2;
// Make text bold
dr[1] = "<b>Boston Crab Meat</b>";
dr[2] = 123;
dataTable.Rows.Add(dr);

// Create export options
ImportTableOptions exportOptions = new ImportTableOptions();
exportOptions.IsFieldNameShown = true;
// Set IsHtmlString property to true as the data contains HTML tags. 
exportOptions.IsHtmlString = true;

// Create workbook
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

worksheet.Cells.ImportData(dataTable, 0, 0, exportOptions);

workbook.Save("HTMLFormattedData_Out.xlsx");
بيانات بتنسيق HTML في جدول بيانات

إخراج بيانات HTML المصدرة إلى مستند Excel

تصدير ملف HTML إلى Excel في C

Aspose.Cells يسمح لنا بتصدير ملف HTML إلى Excel. يجب أن يكون ملف HTML موجهًا لبرنامج Microsoft Excel ، أي يجب أن يكون MS-Excel قادرًا على فتحه.

// An HTML file
string filePath = "Book1.html";

// Instantiate LoadOptions specified by the LoadFormat.
HtmlLoadOptions loadOptions = new HtmlLoadOptions(LoadFormat.Html);

// Create a Workbook object and open the HTML file.
Workbook wb = new Workbook(filePath, loadOptions);

// Save the file as Excel Document
wb.Save("Book1_out.xlsx");

تصدير بيانات JSON إلى Excel في C

نحتاج أحيانًا إلى تصدير بيانات JSON إلى مستند Excel. باستخدام Aspose.Cells يمكننا القيام بذلك بسهولة ببضعة أسطر من التعليمات البرمجية. يوفر Aspose.Cells فئة JsonUtility لها طريقة ImportData لتصدير بيانات JSON إلى مستند Excel. تقبل الطريقة ImportData الكائن JsonLayoutOptions كمعامل. تمثل الفئة JsonLayoutOptions خيارات تخطيط JSON ولها الخصائص التالية.

  • ArrayAsTable: Indicates whether the array should be processed as a table.
  • ConvertNumericOrDate: Gets or sets a value that indicates whether the string in JSON is to be converted to numeric or date.
  • DateFormat: Gets and sets the format of the date value.
  • IgnoreArrayTitle: Indicates whether to ignore the title if the property of the object is an array.
  • IgnoreNull: Indicates whether the null value should be ignored.
  • IgnoreObjectTitle: Indicates whether to ignore the title if the property of the object is an object.
  • NumberFormat: Gets and sets the format of the numeric value.
  • TitleStyle: Gets and sets the style of the title.

في رمز المثال التالي ، نقوم بتصدير بيانات JSON إلى ملف Excel في C #.

// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

// Read JSON file
string jsonInput = File.ReadAllText("Sample.json");

// Set Styles
CellsFactory factory = new CellsFactory();
Style style = factory.CreateStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.Font.Color = System.Drawing.Color.BlueViolet;
style.Font.IsBold = true;

// Set JsonLayoutOptions
JsonLayoutOptions options = new JsonLayoutOptions();
options.TitleStyle = style;
options.ArrayAsTable = true;

// Export JSON Data
JsonUtility.ImportData(jsonInput, worksheet.Cells, 0, 0, options);

// Save Excel file
workbook.Save("ExportingJsonData.xlsx");
{
  "quiz": {
    "sport": {
      "q1": {
        "question": "Which one is correct team name in NBA?",
        "answer": "Huston Rocket"
      }
    },
    "maths": {
      "q1": {
        "question": "5 + 7 = ?",
        "answer": "12"
      },
      "q2": {
        "question": "12 - 8 = ?",
        "answer": "4"
      }
    }
  }
}
بيانات JSON إلى مستند Excel

تصدير بيانات JSON إلى Excel

تصدير بيانات CSV إلى Excel في C

ملف القيم المفصولة بفواصل (CSV) هو ملف نصي محدد يستخدم فاصلة لفصل القيم. عادةً ما يخزن ملف CSV البيانات المجدولة (الأرقام والنصوص) بنص عادي ، وفي هذه الحالة سيكون لكل سطر نفس عدد الحقول.

يوضح نموذج التعليمات البرمجية التالي كيف يمكننا فتح ملف CSV وحفظه كملف Excel باستخدام مكتبة Aspose.Cells.

// Instantiate LoadOptions with CSV LoadFormat.
LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV);

// Open CSV file as a Workbook object
Workbook wb = new Workbook("Business-Price.csv", loadOptions);

// Save the file as an Excel Documnt
wb.Save("CSVAsAnExcelDocument.xlsx");
افتح ملف CSV في مستند جدول بيانات

CSV إلى مستند Excel

استنتاج

في هذا المنشور ، رأيت مدى سهولة تصدير البيانات إلى Excel في C # من Array و DataTable و DataView و DataGrid و GridView. لقد رأيت أيضًا كيفية تصدير بيانات HTML و JSON و CSV إلى ورقة عمل Excel. يرجى مراجعة التوثيق لمعرفة المزيد حول هذه الميزات والعديد من الميزات الأخرى التي تقدمها Aspose.Cells API. إذا كانت لديك أي أسئلة ، فلا تتردد في الاتصال بنا من خلال منتدى الدعم.

أنظر أيضا