- Open Workbook with POI.
- Iterate sheets/rows, read cell values and convert types.
- Insert into Access table using addRowFromMap.
Be careful with date cells (POI may return numeric date values), formulas, and blank cells.
Exporting Access tables to CSV and Excel
Exporting to CSV:
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("export.csv"))) { Table t = db.getTable("Employees"); // header writer.write(String.join(",", t.getColumnNames())); writer.newLine(); for (Row r : t) { List<String> vals = new ArrayList<>(); for (String col : t.getColumnNames()) { Object v = r.get(col); vals.add(v == null ? "" : v.toString().replace(""","""")); } writer.write(String.join(",", vals)); writer.newLine(); } }
Exporting to Excel:
- Use Apache POI to create a Workbook, write header row, then write each Access row into cells converting types appropriately.
Handling attachments, memo, and complex data types
- Memo/Long Text: Jackcess exposes these as Strings. Watch for large text sizes.
- Attachment fields (Access 2007+): Jackcess supports attachment columns; the values are stored as complex objects — check the API for Attachment handling (AttachmentData/AttachmentValue classes).
- Binary/blob fields: returned/accepted as byte[].
Performance tips
- Use DatabaseBuilder to open databases with read-only when you don’t need writes.
- For large imports/exports, use transactions where possible; reduce synchronous commits.
- Prefer addRowFromMap over addRow(Object…) when column order is uncertain.
- Use batch processing and reuse objects to reduce GC pressure.
- Consider using UCanAccess if you need indexed SQL queries — it can offer faster search/update patterns for complex queries.
Common pitfalls
- Character encoding: ensure the correct Charset when reading/writing text.
- AutoNumber handling: do not attempt to set AutoNumber values manually unless you know the consequences.
- Concurrency: Access files are not designed for concurrent multi-writer access; coordinate writes.
- Unsupported Access features: some complex Access-specific features (macros, VBA modules) are not handled by Jackcess.
Example project layout
- src/main/java/ – Java code for import/export utilities
- src/main/resources/ – sample CSV/XLSX files for testing
- pom.xml / build.gradle with jackcess and poi dependencies
- README with usage examples and JVM options
Troubleshooting & resources
- Check Jackcess Javadoc for class/method details.
- For SQL-like workflows, use UCanAccess (built on Jackcess).
- If you hit file format or encryption issues, confirm Access file version and encryption type — some formats may not be supported.
This tutorial gives you a practical workflow for reading, writing, importing, exporting, and manipulating Access databases using Jackcess. For specific code snippets adapted to your schema or a sample migration script (CSV→Access or Access→Excel), provide your table layout and I’ll generate a ready-to-run example.
Leave a Reply