فتح ملفات بتنسيقات مختلفة
باستخدام Aspose.Cells يمكنك فتح الملفات بتنسيقات مختلفة.Aspose.Cells يمكن فتح مجموعة من تنسيقات الملفات مثل Microsoft جداول بيانات Excel (XLS ، XLSX ، XLSM ، XLSB) ، SpreadsheetML ، قيم مفصولة بفواصل (CSV) ، ملفات مفصولة بعلامات جدولة أو ملفات مفصولة بعلامات جدولة (TSV) إلخ.
إذا كنت بحاجة إلى معرفة جميع تنسيقات الملفات المدعومة ، فيرجى الرجوع إلى الصفحات التالية: تنسيقات الملفات المدعومة
فتح ملفات بتنسيقات مختلفة
Aspose.Cells يسمح للمطورين بفتح ملفات جداول البيانات بتنسيقات مختلفة مثل SpreadsheetML ، قيم مفصولة بفواصل (CSV) ، قيم مفصولة بعلامات جدولة أو قيم مفصولة بعلامات جدولة (TSV) ، ملفات ODS. لفتح مثل هذه الملفات ، يمكن للمطورين استخدام نفس المنهجية التي يستخدمونها لفتح ملفات مختلفة من إصدارات Microsoft Excel.
فتح SpreadsheetML الملفات
ملفات SpreadsheetML عبارة عن تمثيلات XML لجداول البيانات بما في ذلك جميع المعلومات المتعلقة بها ، مثل التنسيق والصيغ وما إلى ذلك. منذ Microsoft Excel XP ، تمت إضافة خيار تصدير XML إلى Microsoft Excel الذي يقوم بتصدير جداول البيانات الخاصة بك إلى ملفات SpreadsheetML.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening SpreadsheetML Files | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions3 = new LoadOptions(LoadFormat.SpreadsheetML); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbSpreadSheetML = new Workbook(dataDir + "Book3.xml", loadOptions3); | |
Console.WriteLine("SpreadSheetML file opened successfully!"); |
فتح HTML الملفات
Aspose.Cells يسمح لك بفتح ملف HTML في كائن المصنف. يجب أن يكون الملف HTML موجهًا إلى Microsoft Excel ، أي يجب أن يكون MS-Excel قادرًا على فتحه.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir + "Book1.html"; | |
// Instantiate LoadOptions specified by the LoadFormat. | |
HtmlLoadOptions loadOptions = new HtmlLoadOptions(LoadFormat.Html); | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath, loadOptions); | |
// Save the MHT file | |
wb.Save(filePath + "output.xlsx"); |
فتح CSV الملفات
تحتوي ملفات القيم المفصولة بفواصل (CSV) على سجلات حيث يتم فصل القيم بفاصلات. يتم تخزين البيانات كجدول حيث يتم فصل كل عمود عن طريق الفاصلة ويتم اقتباسها بحرف اقتباس مزدوج. إذا احتوت قيمة الحقل على علامة اقتباس مزدوجة ، يتم تخطيها بزوج من علامات الاقتباس المزدوجة. يمكنك أيضًا استخدام Microsoft Excel لتصدير بيانات جدول البيانات إلى CSV.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions4 = new LoadOptions(LoadFormat.Csv); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbCSV = new Workbook(dataDir + "Book_CSV.csv", loadOptions4); | |
Console.WriteLine("CSV file opened successfully!"); |
فتح CSV الملفات واستبدال الحروف غير الصالحة
في Excel ، عند فتح ملف CSV بأحرف خاصة ، يتم استبدال الأحرف تلقائيًا. يتم القيام بنفس الشيء بواسطة Aspose.Cells API والذي هو موضح في مثال الكود الموضح أدناه.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
var filename = sourceDir + "[20180220142533][ASPOSE_CELLS_TEST].csv"; | |
//Load CSV file | |
var workbook = new Workbook(filename, new TxtLoadOptions() { Separator = ';', LoadFilter = new LoadFilter(LoadDataFilterOptions.CellData), CheckExcelRestriction = false, ConvertNumericData = false, ConvertDateTimeData = false }); | |
Console.WriteLine(workbook.Worksheets[0].Name); // (20180220142533)(ASPOSE_CELLS_T | |
Console.WriteLine(workbook.Worksheets[0].Name.Length); // 31 | |
Console.WriteLine("CSV file opened successfully!"); |
استخدام المحلل اللغوي المفضل
هذا ليس ضروريًا دائمًا لاستخدام إعدادات المحلل اللغوي الافتراضية لفتح ملفات CSV. في بعض الأحيان ، لا يؤدي استيراد ملف CSV إلى إنشاء الإخراج المتوقع مثل تنسيق التاريخ ليس كما هو متوقع أو يتم التعامل مع الحقول الفارغة بشكل مختلف. لهذا الغرضTxtLoadOptions.PreferredParsersمتاح لتوفير المحلل اللغوي المفضل لتحليل أنواع البيانات المختلفة حسب المتطلبات. يوضح نموذج التعليمات البرمجية التالي استخدام المحلل اللغوي المفضل.
يمكن تنزيل نموذج ملف المصدر وملفات الإخراج من الروابط التالية لاختبار هذه الميزة.
outputsamplePreferredParser.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
class TextParser : ICustomParser | |
{ | |
public object ParseObject(string value) | |
{ | |
return value; | |
} | |
public string GetFormat() | |
{ | |
return ""; | |
} | |
} | |
class DateParser : ICustomParser | |
{ | |
public object ParseObject(string value) | |
{ | |
DateTime myDate = DateTime.ParseExact(value, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); | |
return myDate; | |
} | |
public string GetFormat() | |
{ | |
return "dd/MM/yyyy"; | |
} | |
} | |
public static void Main() | |
{ | |
// Initialize Text File's LoadFormat | |
LoadFormat oLoadFormat = LoadFormat.Csv; | |
// Initialize Text File's Load options | |
TxtLoadOptions oTxtLoadOptions = new TxtLoadOptions(oLoadFormat); | |
// Specify the separatot character | |
oTxtLoadOptions.Separator = Convert.ToChar(","); | |
// Specify the encoding scheme | |
oTxtLoadOptions.Encoding = System.Text.Encoding.UTF8; | |
// Set the flag to true for converting datetime data | |
oTxtLoadOptions.ConvertDateTimeData = true; | |
// Set the preferred parsers | |
oTxtLoadOptions.PreferredParsers = new ICustomParser[] { new TextParser(), new DateParser() }; | |
// Initialize the workbook object by passing CSV file and text load options | |
Workbook oExcelWorkBook = new Aspose.Cells.Workbook(sourceDir + "samplePreferredParser.csv", oTxtLoadOptions); | |
// Get the first cell | |
Cell oCell = oExcelWorkBook.Worksheets[0].Cells["A1"]; | |
// Display type of value | |
Console.WriteLine("A1: " + oCell.Type.ToString() + " - " + oCell.DisplayStringValue); | |
// Get the second cell | |
oCell = oExcelWorkBook.Worksheets[0].Cells["B1"]; | |
// Display type of value | |
Console.WriteLine("B1: " + oCell.Type.ToString() + " - " + oCell.DisplayStringValue); | |
// Save the workbook to disc | |
oExcelWorkBook.Save(outputDir + "outputsamplePreferredParser.xlsx"); | |
Console.WriteLine("OpeningCSVFilesWithPreferredParser executed successfully.\r\n"); | |
} |
فتح ملفات نصية باستخدام فاصل مخصص
تُستخدم الملفات النصية للاحتفاظ ببيانات جدول البيانات بدون تنسيق. الملف عبارة عن ملف نصي عادي يمكن أن يحتوي على بعض المحددات المخصصة.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir + "Book11.csv"; | |
// Instantiate Text File's LoadOptions | |
TxtLoadOptions txtLoadOptions = new TxtLoadOptions(); | |
// Specify the separator | |
txtLoadOptions.Separator = Convert.ToChar(","); | |
// Specify the encoding type | |
txtLoadOptions.Encoding = System.Text.Encoding.UTF8; | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath, txtLoadOptions); | |
// Save file | |
wb.Save(dataDir+ "output.txt"); |
فتح ملفات محدده بعلامات تبويب
يحتوي الملف المحدد بعلامات جدولة (نص) على بيانات جدول بيانات ولكن بدون أي تنسيق. يتم ترتيب البيانات في صفوف وأعمدة كما هو الحال في الجداول وجداول البيانات. بشكل أساسي ، الملف المحدد بعلامات جدولة هو نوع خاص من ملفات النص العادي مع علامة تبويب بين كل عمود.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening Tab Delimited Files | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions5 = new LoadOptions(LoadFormat.TabDelimited); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbTabDelimited = new Workbook(dataDir + "Book1TabDelimited.txt", loadOptions5); | |
Console.WriteLine("Tab delimited file opened successfully!"); |
فتح ملفات قيم مفصولة بعلامات جدولة (TSV)
يحتوي ملف القيم المفصولة بعلامات جدولة (TSV) على بيانات جدول بيانات ولكن بدون أي تنسيق. هو نفسه مع ملف محدد بعلامات جدولة حيث يتم ترتيب البيانات في صفوف وأعمدة كما هو الحال في الجداول وجداول البيانات.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Tsv); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleTSVFile.tsv", loadOptions); | |
// Using the Sheet 1 in Workbook | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing a cell using its name | |
Cell cell = worksheet.Cells["C3"]; | |
Console.WriteLine("Cell Name: " + cell.Name + " Value: " + cell.StringValue); | |
فتح SXC الملفات
StarOffice Calc مشابه لـ Microsoft Excel ويدعم الصيغ والمخططات والوظائف ووحدات الماكرو. يتم حفظ جداول البيانات التي تم إنشاؤها باستخدام هذا البرنامج بالملحق SXC. يتم استخدام ملف SXC أيضًا لملفات جداول بيانات OpenOffice.org Calc. يمكن Aspose.Cells قراءة ملفات SXC كما هو موضح في نموذج التعليمات البرمجية التالي.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Sxc); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleSXC.sxc", loadOptions); | |
// Using the Sheet 1 in Workbook | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing a cell using its name | |
Cell cell = worksheet.Cells["C3"]; | |
Console.WriteLine("Cell Name: " + cell.Name + " Value: " + cell.StringValue); | |
فتح FODS الملفات
الملف FODS هو جدول بيانات محفوظ في OpenDocument XML بدون أي ضغط. يمكن لـ Aspose.Cells قراءة ملفات FODS كما هو موضح في نموذج التعليمات البرمجية التالي.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Fods); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleFods.fods", loadOptions); | |
Console.WriteLine("FODS file opened successfully!"); | |