进出口数据
将数据导入工作表
数据代表了世界的本来面目。为了理解数据,我们对其进行分析并了解世界。数据变成信息。
执行分析的方法有很多种:将数据放入电子表格并以不同方式对其进行处理是一种常见的方法。使用 Aspose.Cells,可以轻松创建电子表格,从一系列外部来源获取数据并准备用于分析。
本文讨论开发者通过Aspose.Cells接触到的一些数据导入技巧。
使用 Aspose.Cells 导入数据
当您打开带有 Aspose.Cells 的 Excel 文件时,文件中的所有数据都会自动导入。 Aspose.Cells也可以从其他数据源导入数据:
Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类包含集合工作表集合这允许访问 Excel 文件中的每个工作表。工作表由工作表班级。这工作表类提供了Cells收藏。Cells集合为从其他数据源导入数据提供了非常有用的方法。本文解释了如何使用这些方法。
从数组导入
要将数据从数组导入电子表格,请调用Cells收藏。 importArray 方法有许多重载版本,但典型的重载采用以下参数:
- 大批,您要从中导入内容的数组对象。
- 行号,数据将导入到的第一个单元格的行号。
- 列号,数据将导入到的第一个单元格的列号。
- 是垂直的一个布尔值,指定是垂直导入数据还是水平导入数据。
// 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(ImportingFromArray.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Creating an array containing names as string values | |
String[] names = new String[] { "laurence chen", "roman korchagin", "kyle huang" }; | |
// Importing the array of names to 1st row and first column vertically | |
Cells cells = worksheet.getCells(); | |
cells.importArray(names, 0, 0, false); | |
// Saving the Excel file | |
workbook.save(dataDir + "ImportingFromArray_out.xls"); | |
// Printing the name of the cell found after searching worksheet | |
System.out.println("Process completed successfully"); |
从多维数组导入
要将数据从多维数组导入电子表格,请调用相关的 importArray 重载Cells收藏:
// 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(ImportingFromMultiDimensionalArray.class) + "data/"; | |
// Instantiate a new Workbook | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet (default sheet) in the Workbook | |
Cells cells = workbook.getWorksheets().get("Sheet1").getCells(); | |
// Define a multi-dimensional array and store some data into it. | |
String[][] strArray = { { "A", "1A", "2A" }, { "B", "2B", "3B" } }; | |
// Import the multi-dimensional array to the sheet | |
cells.importArray(strArray, 0, 0); | |
// Save the Excel file | |
workbook.save(dataDir + "IFMDA_out.xlsx"); |
从 ArrayList 导入
从中导入数据数组列表到工作表,调用[导入数组列表](https://reference.aspose.com/cells/java/com.aspose.cells/cells#importArrayList(java.util.ArrayList,%20int,%20int,%20boolean) 的方法Cells收藏。这[导入数组列表](https://reference.aspose.com/cells/java/com.aspose.cells/cells#importArrayList(java.util.ArrayList,%20int,%20int,%20boolean)方法采用以下参数:
- 数组列表 , 这数组列表其内容将被导入的对象。
- 行号将从中导入内容的单元格范围的第一个单元格的行号。
- 列号将从中导入数据的第一个单元格的列号。
- 是垂直的是一个布尔值,指定是垂直导入数据还是水平导入数据。
// 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(ImportingFromArrayList.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(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"); | |
// Importing the contents of ArrayList to 1st row and first column | |
// vertically | |
worksheet.getCells().importArrayList(list, 0, 0, true); | |
// Saving the Excel file | |
workbook.save(dataDir + "IFromArrayList_out.xls"); | |
// Printing the name of the cell found after searching worksheet | |
System.out.println("Process completed successfully"); |
从自定义对象导入到合并区域
要将对象集合中的数据导入包含合并单元格的工作表,请使用ImportTableOptions.CheckMergedCells财产。如果Excel模板有合并单元格,设置值ImportTableOptions.CheckMergedCells属性为真。通过导入表选项对象连同列/属性列表一起添加到显示所需对象列表的方法中。下面的代码示例演示了使用ImportTableOptions.CheckMergedCells属性将数据从自定义对象导入到合并的单元格。请参阅附件源Excel文件和输出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(ImportingFromArrayList.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(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"); | |
// Importing the contents of ArrayList to 1st row and first column | |
// vertically | |
worksheet.getCells().importArrayList(list, 0, 0, true); | |
// Saving the Excel file | |
workbook.save(dataDir + "IFromArrayList_out.xls"); | |
// Printing the name of the cell found after searching worksheet | |
System.out.println("Process completed successfully"); |
从 JSON 导入数据
Aspose.Cells提供了Json工具处理类 JSON。Json工具类有一个[导入数据](https://reference.aspose.com/cells/java/com.aspose.cells/jsonutility#importData(java.lang.String,%20com.aspose.cells.Cells,%20int,%20int,%20com.aspose.cells.JsonLayoutOptions) 导入JSON数据的方法。 Aspose.Cells还提供了JsonLayoutOptions表示 JSON 布局选项的类。这[导入数据](https://reference.aspose.com/cells/java/com.aspose.cells/jsonutility#importData(java.lang.String,%20com.aspose.cells.Cells,%20int,%20int,%20com.aspose.cells.JsonLayoutOptions) 方法接受JsonLayoutOptions作为参数。这JsonLayoutOptions类提供以下属性。
- ArrayAsTable:表示数组中是否作为表格处理。
- 转换数字或日期获取或设置一个值,该值指示JSON中的字符串是转换为数字还是日期。
- 日期格式:获取和设置日期值的格式。
- 忽略数组标题表示如果对象的属性是数组,是否忽略标题
- 忽略空值:指示是否应忽略空值。
- 忽略对象标题表示如果对象的属性是对象,是否忽略标题。
- 数字格式:获取和设置数值的格式。
- 标题样式获取和设置标题的样式。
下面给出的示例代码演示了使用Json工具和JsonLayoutOptions导入 JSON 数据的类。
// 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(ImportingFromJson.class) + "Data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Read File | |
File file = new File(dataDir + "Test.json"); | |
BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); | |
String jsonInput = ""; | |
String tempString; | |
while ((tempString = bufferedReader.readLine()) != null) { | |
jsonInput = jsonInput + tempString; | |
} | |
bufferedReader.close(); | |
// Set Styles | |
CellsFactory factory = new CellsFactory(); | |
Style style = factory.createStyle(); | |
style.setHorizontalAlignment(TextAlignmentType.CENTER); | |
style.getFont().setColor(Color.getBlueViolet()); | |
style.getFont().setBold(true); | |
// Set JsonLayoutOptions | |
JsonLayoutOptions options = new JsonLayoutOptions(); | |
options.setTitleStyle(style); | |
options.setArrayAsTable(true); | |
// Import JSON Data | |
JsonUtility.importData(jsonInput, worksheet.getCells(), 0, 0, options); | |
// Save Excel file | |
workbook.save(dataDir + "ImportingFromJson.out.xlsx"); |
从工作表导出数据
Aspose.Cells 不仅允许其用户将数据从外部数据源导入工作表,还允许他们将工作表数据导出到数组。
使用 Aspose.Cells 导出数据 - 将数据导出到数组
Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类包含一个工作表集合允许访问 Excel 文件中的每个工作表。工作表由工作表班级。这工作表类提供了Cells收藏。
使用以下方法可以轻松地将数据导出到数组对象Cells班级'[导出数组](https://reference.aspose.com/cells/java/com.aspose.cells/cells#exportArray(int,%20int,%20int,%20int)) 方法。
包含强类型数据的列
电子表格将数据存储为一系列行和列。使用[导出数组](https://reference.aspose.com/cells/java/com.aspose.cells/cells#exportArray(int,%20int,%20int,%20int) 方法将数据从工作表导出到数组。导出数组 采用以下参数将工作表数据导出为大批目的:
- 行号,数据将从中导出的第一个单元格的行号。
- 列号,数据将从中导出的第一个单元格的列号
- Number of rows,要导出的行数。
- Number of columns,要导出的列数。
// 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(ExportingDataFromWorksheets.class) + "Data/"; | |
// Creating a file stream containing the Excel file to be opened | |
FileInputStream fstream = new FileInputStream(dataDir + "book1.xls"); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Exporting the contents of 7 rows and 2 columns starting from 1st cell | |
// to Array. | |
Object dataTable[][] = worksheet.getCells().exportArray(0, 0, 7, 2); | |
// Printing the number of rows exported | |
System.out.println("No. Of Rows Exported: " + dataTable.length); | |
// Closing the file stream to free all resources | |
fstream.close(); |