Reputation: 602
I found a great library called Jackcess, allows you to convert, parse create, etc with Microsoft Access.
The aim is to convert, which this code does successfully.
There is a Filter functionality upon export, this can be seen on the link to the documentation below. The aim is to use the first 3 columns, exclude the rest of the data.
Applying the filter object doesn't work, does anyone know if there's something else that needs to be ahcieved first... * scratching my head here *
public void db_dump(String mdbFile, String outputDir) {
File file = new File("/Users/testUser/Downloads/example.mdb");
if(file != null) {
File outDir = new File("/Users/testUser/Desktop/output123");
boolean success = outDir.mkdir();
if (success) {
Database db = null;
try {
db = DatabaseBuilder.open(file);
Table t = db.getTable("MappedCHTCP");
List<Column> cols = new List<Column>()
@Override methods for list ommited .... size(), contains(), etc
System.out.println(t.getColumns());
// cols.add(0,t.getColumn("word"));
for (Column c : t.getColumns()) {
if((c != null) && (c.getColumnIndex() < 3)) {
System.out.println(c.getName());
cols.add(c);
}
}
SimpleExportFilter ef = new SimpleExportFilter(); //THIS IS THE PROBLEM
ef.filterColumns(cols);
File csvFile = new File(outDir+File.separator+"MappedCHTCP.csv");
ExportUtil.exportFile(db, "MappedCHTCP", csvFile, false, null, '"',ef); //NOT ABLE TO APPLY FILTER
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Upvotes: 2
Views: 564
Reputation: 123654
Based on what I found in the unit test code here, the following sample code seems to work. It exports only the first three (3) columns of the table named [Members]:
package jackcessTest;
import java.io.File;
import java.util.*;
import com.healthmarketscience.jackcess.*;
import com.healthmarketscience.jackcess.util.ExportFilter;
import com.healthmarketscience.jackcess.util.ExportUtil;
import com.healthmarketscience.jackcess.util.SimpleExportFilter;
public class JackcessTest {
public static void main(String[] args) {
try (Database db = DatabaseBuilder.open(
new File("C:/Users/Public/mdbTest.mdb"))) {
ExportFilter eFilter = new SimpleExportFilter() {
private List<Column> _cols = new ArrayList<Column>();
private int _colIdx = 0;
@Override
public List<Column> filterColumns(List<Column> columns) {
for (Column c : columns) {
if (_colIdx++ < 3) _cols.add(c);
}
return _cols;
}
};
ExportUtil.exportFile(
db,
"Members",
new File("C:/Users/Public/zzzJdump.csv"),
true,
",",
'"',
eFilter);
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
Upvotes: 3