AnyLogic中导出datasetHourly至Excel文件的技术问询
datasetHourly from AnyLogic to Excel (Bypassing 1000-Row Limit) Hey there! I’ve dealt with this exact AnyLogic dataset export limitation before—super annoying when you’ve got tons of hourly data to analyze. Let’s break down the solutions to get every last row into Excel:
1. First: Confirm Your Dataset Actually Has More Than 1000 Rows
The AnyLogic dataset UI only shows 1000 rows by default, but the full data is usually still stored. Verify this first by adding a quick trace in your model (e.g., in the Main > On stop event):
traceln("Total rows in datasetHourly: " + datasetHourly.getRowCount());
If the output is higher than 1000, you just need a proper export method—your data is there!
2. Use AnyLogic’s Built-in exportToExcel() Method
The easiest way is to leverage the dataset’s native export function, which doesn’t have the 1000-row cap (that’s just the UI limit). Add this code to a trigger that runs after your simulation finishes (like the On stop event):
// Export to Excel (replace path with your desired location) datasetHourly.exportToExcel("./output/hourly_full_data.xlsx", false);
- The first argument is the file path (use relative or absolute; create the
outputfolder first if needed) - The second argument (
false) overwrites the file if it exists; set totrueto append data instead
3. Fallback: Manual Export with Apache POI (For Full Control)
If the built-in method doesn’t work for some reason, use Apache POI (which comes pre-installed with AnyLogic) to write every row directly. Add this code to your simulation’s end trigger:
import org.apache.poi.xssf.usermodel.*; import java.io.*; // Create a new Excel workbook and sheet XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Hourly Dataset"); // Write column headers Row headerRow = sheet.createRow(0); List<String> columnNames = datasetHourly.getColumnNames(); for (int col = 0; col < columnNames.size(); col++) { headerRow.createCell(col).setCellValue(columnNames.get(col)); } // Write all data rows for (int rowNum = 0; rowNum < datasetHourly.getRowCount(); rowNum++) { Row dataRow = sheet.createRow(rowNum + 1); List<Object> rowData = datasetHourly.getRow(rowNum); for (int col = 0; col < rowData.size(); col++) { Object value = rowData.get(col); // Handle different data types properly if (value instanceof Number) { dataRow.createCell(col).setCellValue(((Number) value).doubleValue()); } else if (value instanceof Boolean) { dataRow.createCell(col).setCellValue((Boolean) value); } else { dataRow.createCell(col).setCellValue(value != null ? value.toString() : ""); } } } // Save the workbook to file try (FileOutputStream outputStream = new FileOutputStream("hourly_data_complete.xlsx")) { workbook.write(outputStream); } catch (IOException e) { traceln("Error exporting data: " + e.getMessage()); }
This method gives you full control over formatting and ensures every row is exported, no matter how large your dataset is.
Quick Troubleshooting Tip
If you’re still having issues, make sure your model has write permissions for the output directory. On Windows, avoid saving to restricted folders like C:\Program Files; use your user folder or a dedicated project output folder instead.
内容的提问来源于stack exchange,提问作者CMag

