Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.io.*;
public class AccessDbToExcelFile
{
public static void main(String[] args)
{
HSSFCellStyle style = null;
HSSFWorkbook wb = null;
FileOutputStream fileOut = null;
HSSFSheet sheet1 = null;
HSSFRow row = null;
HSSFCell cell = null;
ResultSet rs = null;
Statement stmt = null;
String excelFileName = "C:\\SampleAccessToExcel.xls";
try
{
//Creating a new Excel Workbook
wb = new HSSFWorkbook();
fileOut = new FileOutputStream(excelFileName);
System.out.println ("Created Excel Workbook: "+ excelFileName);
//Creating a new sheet1 inside the workbook
wb = new HSSFWorkbook();
sheet1 = wb.createSheet("Data From AccessDB");
System.out.println("Finshed creating a sheet1 within the workbook");
}
catch (FileNotFoundException fnfe)
{
System.out.println ("FileNotFoundException " + fnfe);
}
//Creating a connection object
Connection aConnection = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//make sure that this is a space between "MicroSoft Access Driver" and "(*.mdb)"
aConnection = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C://TestDB.mdb","","");
stmt = aConnection.createStatement();
String query = "SELECT CompanyInformation.CompanyID, CompanyInformation.CompanyName," +
"CompanyInformation.StreetAddress" +
" FROM CompanyInformation;";
rs = stmt.executeQuery (query);
//Creating a Row for columns headings - POI starts with row 0
row = sheet1.createRow((short)0);
//Get column headings and print them in Excel - on the first row
ResultSetMetaData meta = rs.getMetaData();
int columnNumber = 0;
for (int i = 1; i <= meta.getColumnCount(); i++)
{
String columnName = meta.getColumnName(i);
//Places the column name in the cell - cell are base 0
cell = row.createCell((short)columnNumber);
cell.setCellValue(columnName);
System.out.println(columnName);
columnNumber++;
//Adding some color to the cell
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.TAN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell.setCellStyle(style);
}
//Taking the resultset and printing it to excel.
int rowNumber = 1;
int stringLenght = 1;
while (rs.next())
{
//Used to keep track of the column numbers
int columnCount = 0;
//Creating a new row
row = sheet1.createRow((short)rowNumber);
int companyID = rs.getInt("CompanyID");
//Creating a new cell
cell = row.createCell((short)columnCount);
cell.setCellValue(companyID);
columnCount++;
String CompanyName = rs.getString("CompanyName");
cell = row.createCell((short)columnCount);
columnCount++;
String StreetAddress = rs.getString("StreetAddress");
cell = row.createCell((short)columnCount);
rowNumber++;
}
System.out.println("Added " + rowNumber + " new row to Excel Workbook: " + excelFileName);
}
catch (ClassNotFoundException cnfe)
{
System.out.println("ClassNotFoundException " + cnfe);
}
catch (Exception ex)
{
System.out.println ("**General Exception**");
ex.printStackTrace();
}
finally
{
try
{
rs.close();
aConnection.close();
stmt.close();
wb.write(fileOut);
fileOut.close();
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe);
}
catch(SQLException sqlE)
{
System.out.println("SQLException " + sqlE);
}
}
}
}
ResultSet