将遗留 Microsoft Access 数据库迁移到 Excel(或其他任何数据库)- 完整应用程序和代码
帮助您将 Microsoft Access 数据库迁移到 Excel
引言
最近,我有一个客户使用 Visual Basic 创建的遗留应用程序,并使用 Microsoft Access,希望迁移数据库以支持新的基于云的 Web 应用程序。
整个想法是使用原始数据的新应用程序。
背景
最初的要求是将所有数据导出到一种格式,然后可以将其导出到任何数据库。这是一个一次性过程,旧系统预计在新基于云的 Web 应用程序准备好后将被弃用。我们提出一个控制台应用程序,它会将数据导出到 Excel/CSV,然后可以将其用于将数据导入任何数据库。
Using the Code
这是一个小代码,它遵循以下步骤来迁移数据
- 连接到 Access 数据库并获取表列表。
- 循环遍历表并将每个表导出到单独的工作表。
- 逐个单元格转换数据,您可以在此处添加验证或转换数据。
使用的库
- Microsoft.Office.Interop.Excel:简化对 Office API 对象的访问
System.Data.OleDb
:.NET Framework 数据提供程序,用于 OLE DB,以连接到 Access 数据库,但您可以使用此方法连接到遗留数据库
//Get all data from the Source database
DataSet ds = GetAllDataFromSource();
//Export all data to Excel
ExportDataSetToExcel(ds);
步骤 1
从源数据库获取所有数据
private static DataSet GetAllDataFromSource()
{
//Declare
System.Data.DataTable userTables = null;
OleDbDataAdapter oledbAdapter;
DataSet ds = new DataSet();
List<string> tableNames = new List<string>();
using (OleDbConnection myConnection = new OleDbConnection())
{
myConnection.ConnectionString = ConfigurationManager.ConnectionStrings
["SourceDatabaseConnectionString"].ConnectionString;
//Connect to Source database
myConnection.Open();
//Restrict the GetSchema() to return "Tables" schema information only.
string[] restrictions = new string[4];
restrictions[3] = "Table";
userTables = myConnection.GetSchema("Tables", restrictions);
for (int i = 0; i < userTables.Rows.Count; i++)
{
var tableName = userTables.Rows[i][2].ToString();
oledbAdapter = new OleDbDataAdapter($"select * from {tableName}", myConnection);
oledbAdapter.Fill(ds, $"{tableName}");
if (ds.Tables[$"{tableName}"].Rows.Count > 0)
{
Console.WriteLine("Rows: " + ds.Tables[$"{tableName}"].Rows.Count);
}
oledbAdapter.Dispose();
}
myConnection.Close();
}
return ds;
}
第二步
将数据导出到 Excel
private static void ExportDataSetToExcel(DataSet ds)
{
//Create an Excel application instance
Excel.Application excelApp = new Excel.Application();
Excel.Workbook excelWorkBook =
excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
//Create an Excel workbook instance and open it from the predefined location
foreach (System.Data.DataTable table in ds.Tables)
{
//Add a new worksheet to workbook with the Datatable name
Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
excelWorkSheet.Name = table.TableName;
//Columns
for (int i = 1; i < table.Columns.Count + 1; i++)
{
excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
}
//Rows
for (int j = 0; j < table.Rows.Count; j++)
{
for (int k = 0; k < table.Columns.Count; k++)
{
try
{
excelWorkSheet.Cells[j + 2, k + 1] =
table.Rows[j].ItemArray[k].ToString();
}
catch(Exception ex)
{
Console.WriteLine($"Error in table:
{excelWorkSheet.Name} - Cells - j: {j},
k:{k}, data: {table.Rows[j].ItemArray[k].ToString()}");
Console.WriteLine(ex);
}
}
}
}
string fileName = System.IO.Path.Combine
(System.Configuration.ConfigurationManager.AppSettings
["TargetDirectory"], $@"test-{DateTime.Now.ToString
("yyyyMMddHHmmss")}.xls");
excelWorkBook.SaveAs(fileName);
excelWorkBook.Close();
excelApp.Quit();
}
结果
欢迎在 Github 上修改/扩展此代码:https://github.com/rohitsies/DataExportFromMSAccess/blob/master/README.md。
关注点
Microsoft.Office.Interop
是与 Excel 和其他 Office 应用程序交互的绝佳工具。它提供无缝集成,节省时间和精力。
历史
- 2020 年 6 月 1 日:发布初始文章
- 2020 年 6 月 2 日:更正了格式