I am searching for a tutorial or code to help me understand how to export an access db, with multiple tables to an excel spreadsheet, with each table having its own sheet.
Loading
I am searching for a tutorial or code to help me understand how to export an access db, with multiple tables to an excel spreadsheet, with each table having its own sheet.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 16, 2024, 2:56 AM EDT
To export multiple tables from an Access database to an Excel spreadsheet with each table on a separate sheet, you can use C# and the ADO.NET library.
using System; using System.Data; using System.Data.OleDb; using Excel = Microsoft.Office.Interop.Excel; namespace AccessToExcelExporter { class Program { static void Main(string[] args) { string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\path\\to\\your\\database.accdb;"; using (OleDbConnection connection = new OleDbConnection(connectionString)) { connection.Open(); DataTable tables = connection.GetSchema("Tables"); Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Add(); foreach (DataRow table in tables.Rows) { string tableName = table["TABLE_NAME"].ToString(); OleDbDataAdapter adapter = new OleDbDataAdapter($"SELECT * FROM [{tableName}]", connection); DataTable data = new DataTable(); adapter.Fill(data); Excel.Worksheet worksheet = workbook.Sheets.Add(); worksheet.Name = tableName; for (int i = 0; i < data.Rows.Count; i++) { for (int j = 0; j < data.Columns.Count; j++) { worksheet.Cells[i + 1, j + 1] = data.Rows[i][j].ToString(); } } } workbook.SaveAs("C:\\path\\to\\save\\output.xlsx"); workbook.Close(); excelApp.Quit(); } } } }we establish a connection to the Access database, retrieve the table names, and then export each table to a separate sheet in an Excel workbook.
Tahir AnsariRank #366 · 6.8K reputationPosted Apr 15, 2024, 3:04 PM EDT
using System; using System.Data.OleDb; using Excel = Microsoft.Office.Interop.Excel; namespace AccessToExcel { class Program { static void Main(string[] args) { string accessDbPath = "YourAccessDBPath.accdb"; string excelFilePath = "YourExcelFilePath.xlsx"; ExportToExcel(accessDbPath, excelFilePath); } static void ExportToExcel(string accessDbPath, string excelFilePath) { // Connection string for Access database string connStr = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={accessDbPath};"; // Create an instance of Excel application Excel.Application excelApp = new Excel.Application(); excelApp.Visible = false; // Create a new workbook Excel.Workbook workbook = excelApp.Workbooks.Add(); using (OleDbConnection conn = new OleDbConnection(connStr)) { conn.Open(); // Retrieve table names from Access database DataTable tables = conn.GetSchema("Tables"); foreach (DataRow row in tables.Rows) { string tableName = row["TABLE_NAME"].ToString(); // Create a new worksheet for each table Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.Add(); worksheet.Name = tableName; // Retrieve data from Access table string sql = $"SELECT * FROM [{tableName}]"; OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn); DataTable dt = new DataTable(); adapter.Fill(dt); // Write data to Excel worksheet for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; } for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dt.Rows[i][j]; } } } conn.Close(); } // Save Excel workbook workbook.SaveAs(excelFilePath); workbook.Close(); excelApp.Quit(); } } }RICHARD KNOX100 reputationPosted Apr 17, 2024, 7:30 AM EDT
I tried the first answer and received this error.
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 17, 2024, 8:12 AM EDT
Sure, I can guide you on how to export data from an Access database to Excel. Each table in the Access database can be exported to a separate sheet in an Excel workbook. Here’s a step-by-step guide:
Please note that this process exports each table to a separate Excel file. If you want each table in a separate sheet within the same Excel file, you would need to manually move or copy the sheets into one workbook after the export process.
Also, keep in mind that calculated fields, totals, and controls created in Access will export as values, not as calculations3. When you export a form, report, or datasheet that contains subforms, subreports, or subdatasheets, only the main form, report, or datasheet is exported. You must repeat the export operation for each subform, subreport, and subdatasheet that you want to export to Excel3.
Thanks
RICHARD KNOX100 reputationPosted Apr 17, 2024, 10:19 AM EDT
I am trying to export the tables from an access db to excel, programically thru C#, not just utilizing the application.
RICHARD KNOX100 reputationPosted Apr 17, 2024, 11:19 AM EDT
I resolved my previous issue, but I now have a new issue. Been reading thru a lot of posts, but am unable to figure this one out.
RICHARD KNOX100 reputationPosted Apr 17, 2024, 3:40 PM EDT
private void button1_Click(object sender, EventArgs e) { using (AC.con) AC.OpenConnection(); string[] restrictionValues = new string[4]; restrictionValues[3] = "Table"; DataTable tables = AC.con.GetSchema("Tables", restrictionValues); Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Add(); foreach (DataRow table in tables.Rows) { string tableName = table["TABLE_NAME"].ToString(); OleDbDataAdapter adapter = new OleDbDataAdapter($"SELECT * FROM [{tableName}]", AC.con); DataTable data = new DataTable(); adapter.Fill(data); Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets.Add(); worksheet.Name = tableName; DataTable dt = new DataTable(); adapter.Fill(dt); //Write Column names to Excel worksheet for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; } //Write Column data to Excel worksheet for (int i = 0; i < data.Rows.Count; i++) { for (int j = 0; j < data.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = data.Rows[i][j].ToString(); } } } workbook.SaveAs(@Properties.Settings.Default.Backup + Properties.Settings.Default.Backup_Name + ".xlsx"); workbook.Close(); excelApp.Quit(); AC.CloseConnection(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); MessageBox.Show("Completed"); } }I figured out what I needed to do. Here is the code I created, please let me know if I missed anything or anything I could have done better.