قم بتحويل CSV و TSV و TXT إلى Excel
فتح 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."); |