فتح ملفات بتنسيقات مختلفة
يستخدم المطورون Aspose.Cells لفتح الملفات لأغراض مختلفة. على سبيل المثال ، افتح ملفًا لاسترداد البيانات منه ، أو استخدم ملف جدول بيانات مصمم مسبقًا لتسريع عملية التطوير لديك. Aspose.Cells يسمح للمطورين بفتح أنواع مختلفة من الملفات المصدر. يمكن أن تكون ملفات المصدر هذه Microsoft تقارير Excel أو SpreadsheetML أو قيم مفصولة بفواصل (CSV) أو ملفات مفصولة بعلامات جدولة أو قيم مفصولة بعلامات جدولة (TSV). تتناول هذه المقالة فتح هذه الملفات المصدر المختلفة باستخدام Aspose.Cells.
إذا كنت بحاجة إلى معرفة جميع تنسيقات الملفات المدعومة ، فيرجى الرجوع إلى الصفحات التالية: تنسيقات الملفات المدعومة
طرق بسيطة لفتح ملفات Excel
فتح طريق
لفتح ملف Excel Microsoft باستخدام مسار الملف ، قم بتمرير مسار الملف كمعامل أثناء إنشاء مثيل لـ**[مصنف] (https://reference.aspose.com/cells/java/com.aspose.cells/Workbook)**صف دراسي. يوضح نموذج التعليمات البرمجية التالي فتح ملف Excel باستخدام مسار الملف.
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningFilesThroughPath.class) + "files/"; | |
// Opening from path. | |
// Creating an Workbook object with an Excel file path | |
Workbook workbook1 = new Workbook(dataDir + "Book1.xlsx"); | |
// Print message | |
System.out.println("Workbook opened using path successfully."); |
فتح من خلال تيار
في بعض الأحيان ، يتم تخزين ملف Excel الذي تريد فتحه كتدفق. في هذه الحالة ، على غرار فتح ملف باستخدام مسار الملف ، قم بتمرير الدفق كمعامل أثناء إنشاء ملف**[مصنف] (https://reference.aspose.com/cells/java/com.aspose.cells/Workbook)** صف دراسي. يوضح نموذج التعليمات البرمجية التالي فتح ملف Excel باستخدام دفق.
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningFilesThroughStream.class) + "loading_saving/"; | |
// Opening workbook from stream | |
// Create a Stream object | |
FileInputStream fstream = new FileInputStream(dataDir + "Book2.xls"); | |
// Creating an Workbook object with the stream object | |
Workbook workbook2 = new Workbook(fstream); | |
fstream.close(); | |
// Print message | |
System.out.println("Workbook opened using stream successfully."); |
فتح ملفات مختلفة من إصدارات إكسل Microsoft
يجوز للمستخدم استخدام**[LoadOptions] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadOptions)** فئة لتحديد تنسيق ملف Excel باستخدام امتداد**[LoadFormat] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadFormat)**تعداد.
ال**[LoadFormat] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadFormat)**يحتوي التعداد على العديد من تنسيقات الملفات المحددة مسبقًا والتي يرد بعضها أدناه.
أنواع التنسيق | وصف |
---|---|
Csv | يمثل ملف CSV |
إكسل 97 إلى 2003 | يمثل ملف Excel 97 - 2003 |
xlsx | يمثل ملف Excel 2007/2010/2013/2016/2019 و Office 365 XLSX |
Xlsm | يمثل ملف Excel 2007/2010/2013/2016/2019 و Office 365 XLSM |
Xltx | يمثل ملف Excel 2007/2010/2013/2016/2019 ونموذج Office 365 XLTX |
Xltm | يمثل ملف XLTM ممكّن للماكرو في Excel 2007/2010/2013/2016/2019 و Office 365 |
Xlsb | يمثل ملف Excel 2007/2010/2013/2016/2019 و Office 365 ثنائي XLSB |
SpreadsheetML | يمثل ملف SpreadsheetML |
Tsv | يمثل ملف قيم مفصولة بعلامات جدولة |
TabDelimited | يمثل ملفًا نصيًا محددًا بعلامات جدولة |
أود | يمثل ملف ODS |
لغة البرمجة | يمثل ملف HTML |
Mhtml | يمثل ملف MHTML |
فتح Microsoft ملفات Excel 95 / 5.0
لفتح ملفات Microsoft Excel 95 ، قم بإنشاء مثيل لملف**[مصنف] (https://reference.aspose.com/cells/java/com.aspose.cells/Workbook)**المثيل مع مسار أو دفق ملف القالب. يمكن تنزيل ملف نموذج لاختبار الكود من الرابط التالي:
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Opening Microsoft Excel 97 Files | |
// Creating an EXCEL_97_TO_2003 LoadOptions object | |
// Creating an Workbook object with excel 97 file path and the | |
// loadOptions object | |
new Workbook(srcDir + "Excel95_5.0.xls"); | |
فتح Microsoft Excel 97 أو الإصدارات الأحدث XLS الملفات
لفتح XLS ملفات Microsoft Excel XLS 97 أو الإصدارات الأحدث ، قم بإنشاء مثيل لـ**[مصنف] (https://reference.aspose.com/cells/java/com.aspose.cells/Workbook)**المثيل مع مسار أو دفق ملف القالب. أو استخدم ملف**[LoadOptions] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadOptions)** الطريقة وحدد**[EXCEL_97_TO_2003] (https://reference.aspose.com/cells/java/com.aspose.cells/loadformat#EXCEL_97_TO_2003)** قيمة في**[LoadFormat] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadFormat)**تعداد.
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningMicrosoftExcel972003Files.class) + "loading_saving/"; | |
// Opening Microsoft Excel 97 Files | |
// Createing and EXCEL_97_TO_2003 LoadOptions object | |
LoadOptions loadOptions1 = new LoadOptions(LoadFormat.EXCEL_97_TO_2003); | |
// Creating an Workbook object with excel 97 file path and the | |
// loadOptions object | |
Workbook workbook3 = new Workbook(dataDir + "Book_Excel97_2003.xls", loadOptions1); | |
// Print message | |
System.out.println("Excel 97 Workbook opened successfully."); |
فتح Microsoft Excel 2007 أو الإصدارات الأحدث XLSX الملفات
لفتح XLSX ملفات Microsoft Excel 2007 أو الإصدارات الأحدث ، قم بإنشاء مثيل**[مصنف] (https://reference.aspose.com/cells/java/com.aspose.cells/Workbook)**المثيل مع مسار أو دفق ملف القالب. أو استخدم ملف**[LoadOptions] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadOptions)** فئة واختيار**[XLSX] (https://reference.aspose.com/cells/java/com.aspose.cells/loadformat#XLSX)** قيمة في**[LoadFormat] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadFormat)**تعداد.
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningMicrosoftExcel2007XlsxFiles.class) + "loading_saving/"; | |
// Opening Microsoft Excel 2007 XLSX Files. Createing and XLSX LoadOptions object | |
LoadOptions loadOptions2 = new LoadOptions(LoadFormat.XLSX); | |
// Creating an Workbook object with 2007 xlsx file path and the loadOptions object | |
Workbook workbook4 = new Workbook(dataDir + "Book_Excel2007.xlsx", loadOptions2); | |
// Print message | |
System.out.println("Excel 2007 Workbook opened successfully."); |
فتح ملفات بتنسيقات مختلفة
Aspose.Cells يسمح للمطورين بفتح ملفات جداول البيانات بتنسيقات مختلفة مثل SpreadsheetML ، CSV ، الملفات المحددة بعلامات جدولة. لفتح مثل هذه الملفات ، يمكن للمطورين استخدام نفس المنهجية التي يستخدمونها لفتح ملفات مختلفة من إصدارات Microsoft Excel.
فتح SpreadsheetML الملفات
ملفات SpreadsheetML هي تمثيلات XML لجداول البيانات الخاصة بك بما في ذلك جميع المعلومات حول جدول البيانات مثل التنسيق والصيغ وما إلى ذلك. منذ Microsoft Excel XP ، تمت إضافة خيار تصدير XML إلى Microsoft Excel الذي يقوم بتصدير جداول البيانات الخاصة بك إلى ملفات SpreadsheetML.
لفتح SpreadsheetML ملفات ، استخدم امتداد**[LoadOptions] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadOptions)** فئة واختيار**[SPREADSHEET_ML] (https://reference.aspose.com/cells/java/com.aspose.cells/loadformat#SPREADSHEET_ML)** قيمة في**[LoadFormat] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadFormat)**تعداد.
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningSpreadsheetMLFiles.class) + "loading_saving/"; | |
// Opening SpreadsheetML Files | |
// Creating and EXCEL_2003_XML LoadOptions object | |
LoadOptions loadOptions3 = new LoadOptions(LoadFormat.SPREADSHEET_ML); | |
// Creating an Workbook object with SpreadsheetML file path and the | |
// loadOptions object | |
Workbook workbook5 = new Workbook(dataDir + "Book3.xml", loadOptions3); | |
// Print message | |
System.out.println("SpreadSheetML format workbook has been opened successfully."); |
فتح CSV الملفات
تحتوي ملفات القيم المفصولة بفواصل (CSV) على سجلات محددة قيمها أو مفصولة بفواصل. في ملفات CSV ، يتم تخزين البيانات في تنسيق جدولي يحتوي على حقول مفصولة بفاصلة ويتم اقتباسها بحرف اقتباس مزدوج. إذا احتوت قيمة الحقل على علامة اقتباس مزدوجة ، يتم تخطيها بزوج من علامات الاقتباس المزدوجة. يمكنك أيضًا استخدام Microsoft Excel لتصدير بيانات جدول البيانات إلى ملف CSV.
لفتح CSV ملفات ، استخدم امتداد**[LoadOptions] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadOptions)** فئة واختيار**[CSV] (https://reference.aspose.com/cells/java/com.aspose.cells/loadformat#CSV)** القيمة المحددة مسبقًا في**[LoadFormat] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadFormat)**تعداد.
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningCSVFiles.class) + "loading_saving/"; | |
// Opening CSV Files | |
// Creating and CSV LoadOptions object | |
LoadOptions loadOptions4 = new LoadOptions(LoadFormat.CSV); | |
// Creating an Workbook object with CSV file path and the loadOptions | |
// object | |
Workbook workbook6 = new Workbook(dataDir + "Book_CSV.csv", loadOptions4); | |
// Print message | |
System.out.println("CSV format workbook has been 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-Java | |
//Source directory | |
String dataDir = Utils.getSharedDataDir(OpeningCSVFilesAndReplacingInvalidCharacters.class) + "LoadingSavingConvertingAndManaging/"; | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV); | |
//Load CSV file | |
Workbook workbook = new Workbook(dataDir + "[20180220142533][ASPOSE_CELLS_TEST].csv", loadOptions); | |
System.out.println(workbook.getWorksheets().get(0).getName()); // (20180220142533)(ASPOSE_CELLS_T | |
System.out.println(workbook.getWorksheets().get(0).getName().length()); // 31 | |
System.out.println("CSV file opened successfully!"); |
فتح CSV الملفات باستخدام المحلل اللغوي المفضل
هذا ليس ضروريًا دائمًا لاستخدام إعدادات المحلل اللغوي الافتراضية لفتح ملفات CSV. في بعض الأحيان ، لا يؤدي استيراد ملف CSV إلى إنشاء الإخراج المتوقع مثل تنسيق التاريخ ليس كما هو متوقع أو يتم التعامل مع الحقول الفارغة بشكل مختلف. لهذا الغرض**[TxtLoadOptions.PreferredParsers] (https://reference.aspose.com/cells/java/com.aspose.cells/txtloadoptions#PreferredParsers)**متاح لتوفير المحلل اللغوي المفضل لتحليل أنواع البيانات المختلفة حسب المتطلبات. يوضح نموذج التعليمات البرمجية التالي استخدام المحلل اللغوي المفضل.
يمكن تنزيل نموذج ملف المصدر وملفات الإخراج من الروابط التالية لاختبار هذه الميزة.
outputsamplePreferredParser.xlsx
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
class TextParser implements ICustomParser | |
{ | |
@Override | |
public Object parseObject(String s) { | |
return s; | |
} | |
@Override | |
public String getFormat() { | |
return ""; | |
} | |
} | |
class DateParser implements ICustomParser { | |
@Override | |
public Object parseObject(String s) { | |
Date myDate = null; | |
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); | |
try { | |
myDate = formatter.parse(s); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return myDate; | |
} | |
@Override | |
public String getFormat() { | |
return "dd/MM/yyyy"; | |
} | |
} | |
public class OpeningCSVFilesWithPreferredParser { | |
//Source directory | |
private static String sourceDir = Utils.Get_SourceDirectory(); | |
private static String outputDir = Utils.Get_OutputDirectory(); | |
public static void main(String[] args) throws Exception { | |
// Initialize Text File's Load options | |
TxtLoadOptions oTxtLoadOptions = new TxtLoadOptions(LoadFormat.CSV); | |
// Specify the separatot character | |
oTxtLoadOptions.setSeparator(','); | |
// Specify the encoding scheme | |
oTxtLoadOptions.setEncoding(Encoding.getUTF8()); | |
// Set the flag to true for converting datetime data | |
oTxtLoadOptions.setConvertDateTimeData(true); | |
// Set the preferred parsers | |
oTxtLoadOptions.setPreferredParsers(new ICustomParser[] { new TextParser(), new DateParser() }); | |
// Initialize the workbook object by passing CSV file and text load options | |
Workbook oExcelWorkBook = new Workbook(sourceDir + "samplePreferredParser.csv", oTxtLoadOptions); | |
// Get the first cell | |
Cell oCell = oExcelWorkBook.getWorksheets().get(0).getCells().get("A1"); | |
// Display type of value | |
System.out.println("A1: " + getCellType(oCell.getType()) + " - " + oCell.getDisplayStringValue()); | |
// Get the second cell | |
oCell = oExcelWorkBook.getWorksheets().get(0).getCells().get("B1"); | |
// Display type of value | |
System.out.println("B1: " + getCellType(oCell.getType()) + " - " + oCell.getDisplayStringValue()); | |
// Save the workbook to disc | |
oExcelWorkBook.save(outputDir + "outputsamplePreferredParser.xlsx"); | |
System.out.println("OpeningCSVFilesWithPreferredParser executed successfully.\r\n"); | |
} | |
private static String getCellType(int type){ | |
if(type == CellValueType.IS_STRING){ | |
return "String"; | |
} else if(type == CellValueType.IS_NUMERIC){ | |
return "Numeric"; | |
} else if(type == CellValueType.IS_BOOL){ | |
return "Bool"; | |
} else if(type == CellValueType.IS_DATE_TIME){ | |
return "Date"; | |
} else if(type == CellValueType.IS_NULL){ | |
return "Null"; | |
} else if(type == CellValueType.IS_ERROR){ | |
return "Error"; | |
} else{ | |
return "Unknown"; | |
} | |
} |
فتح ملفات TSV (محدد بعلامات جدولة)
تحتوي الملفات المحددة بعلامات جدولة على بيانات جدول بيانات ولكن بدون أي تنسيق. يتم ترتيب البيانات في صفوف وأعمدة مثل الجداول وجداول البيانات. باختصار ، الملف المحدد بعلامات جدولة هو نوع خاص من ملفات النص العادي بعلامة جدولة بين كل عمود في النص.
لفتح ملفات محددة بعلامات جدولة ، يجب على المطورين استخدام الامتداد**[LoadOptions] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadOptions)** فئة واختيار**[TSV] (https://reference.aspose.com/cells/java/com.aspose.cells/loadformat#TSV)** القيمة المحددة مسبقًا في**[LoadFormat] (https://reference.aspose.com/cells/java/com.aspose.cells/LoadFormat)**تعداد.
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningTabDelimitedFiles.class) + "loading_saving/"; | |
// Creating and TAB_DELIMITED LoadOptions object | |
LoadOptions loadOptions5 = new LoadOptions(LoadFormat.TSV); | |
// Creating an Workbook object with Tab Delimited text file path and the | |
// loadOptions object | |
Workbook workbook7 = new Workbook(dataDir + "Book1TabDelimited.txt", loadOptions5); | |
// Print message | |
System.out.println("Tab Delimited workbook has been opened successfully."); |
فتح ملفات اكسل المشفرة
نعلم أنه من الممكن إنشاء ملفات Excel مشفرة باستخدام Microsoft Excel. لفتح مثل هذه الملفات المشفرة ، يجب على المطورين استدعاء طريقة LoadOptions خاصة محملة بشكل زائد وتحديد قيمة DEFAULT المحددة مسبقًا في تعداد FileFormatType. ستأخذ هذه الطريقة أيضًا كلمة المرور للملف المشفر كما هو موضح أدناه في المثال.
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningEncryptedExcelFiles.class) + "loading_saving/"; | |
// Opening Encrypted Excel Files | |
// Creating and EXCEL_97_TO_2003 LoadOptions object | |
LoadOptions loadOptions6 = new LoadOptions(LoadFormat.EXCEL_97_TO_2003); | |
// Setting the password for the encrypted Excel file | |
loadOptions6.setPassword("1234"); | |
// Creating an Workbook object with file path and the loadOptions object | |
Workbook workbook8 = new Workbook(dataDir + "encryptedBook.xls", loadOptions6); | |
// Print message | |
System.out.println("Encrypted workbook has been opened successfully."); |
يدعم Aspose.Cells أيضًا فتح ملفات MS Excel 2013 المحمية بكلمة مرور.
فتح 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-Java | |
// The path to the source directory. | |
String sourceDir = Utils.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.getWorksheets().get(0); | |
// Accessing a cell using its name | |
Cell cell = worksheet.getCells().get("C3"); | |
System.out.println("Cell Name: " + cell.getName() + " Value: " + cell.getStringValue()); |
فتح FODS الملفات
الملف FODS هو جدول بيانات محفوظ في OpenDocument XML بدون أي ضغط. يمكن لـ Aspose.Cells قراءة ملفات FODS كما هو موضح في نموذج التعليمات البرمجية التالي.
مثال
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the source directory. | |
String sourceDir = Utils.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); | |
// Print message | |
System.out.println("FODS file opened successfully!"); |
موضوعات مسبقة
- تصفية الأسماء المعرفة أثناء تحميل المصنف
- تصفية الكائنات أثناء تحميل المصنف أو ورقة العمل
- احصل على تحذيرات أثناء تحميل ملف Excel
- احتفظ بالفواصل للصفوف الفارغة أثناء تصدير جداول البيانات بتنسيق CSV
- تحميل المصنف بحجم ورق طابعة محدد
- فتح ملفات إصدارات Excel Microsoft المختلفة
- تحسين استخدام الذاكرة أثناء العمل مع الملفات الكبيرة التي تحتوي على مجموعات بيانات كبيرة
- اقرأ جدول البيانات Numbers الذي طورته شركة Apple Inc. باستخدام Aspose.Cells
- قراءة CSV ملف ذو ترميزات متعددة
- أوقف التحويل أو التحميل باستخدام InterruptMonitor عندما يستغرق وقتًا طويلاً
- باستخدام LightCells API