ExcelファイルをAccessテーブルにインポートしたい。
初めてうまく機能するコードがあります。Excelファイルのすべてのシートをループし、レコードを挿入します。
2回目は、例外が発生します。
「テーブルはすでに存在します」
テーブルがAccessファイルにすでに存在していることを知っています!もっとレコードを入れたい!落としたくない!
誰もがこれを解決する方法を知っていますか?
これがコードです:
class Program
{
static void Main(string[] args)
{
string ExcelFiles = System.Configuration.ConfigurationSettings.AppSettings["FilesLocation"];
string AccessFile = System.Configuration.ConfigurationSettings.AppSettings["AccessFileLocation"];
string[] files = Directory.GetFiles(ExcelFiles, "*.xlsx");
foreach (string excelFile in files)
{
string[] sheets = ListSheetInExcel(excelFile).ToArray();
foreach (string sheetName in sheets)
{
ImportSpreadsheet(
excelFile,
sheetName.Replace("'", ""),
"MyTable",
AccessFile);
}
}
}
public static void ImportSpreadsheet(string ExcelfileName, string ExcelsheetName, string AccesstableName, string AccessDatabase)
{
OleDbConnectionStringBuilder sbConnection = new OleDbConnectionStringBuilder();
String strExtendedProperties = String.Empty;
sbConnection.DataSource = ExcelfileName;
if (Path.GetExtension(ExcelfileName).Equals(".xls"))//for 97-03 Excel file
{
sbConnection.Provider = "Microsoft.Jet.OLEDB.4.0";
strExtendedProperties = "Excel 8.0;HDR=Yes;IMEX=1";//HDR=ColumnHeader,IMEX=InterMixed
}
else if (Path.GetExtension(ExcelfileName).Equals(".xlsx")) //for 2007 Excel file
{
sbConnection.Provider = "Microsoft.ACE.OLEDB.12.0";
strExtendedProperties = "Excel 12.0;HDR=Yes;IMEX=1";
}
sbConnection.Add("Extended Properties", strExtendedProperties);
using (OleDbConnection conn = new OleDbConnection(sbConnection.ToString()))
{
try
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.CommandText = @"SELECT * INTO [MS Access;Database="
+ AccessDatabase + "].["
+ AccesstableName + "] FROM ["
+ ExcelsheetName + "]";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
cmd.ExecuteNonQuery(); //THE ERROR OCCURS HERE !!!!!!!!
}
}
catch (DbException ex)
{
Console.WriteLine("Exception: {0}\r\n Stack Trace: {1}", ex.Message, ex.StackTrace);
}
finally
{
conn.Close();
}
}
}
public static List<string> ListSheetInExcel(string filePath)
{
OleDbConnectionStringBuilder sbConnection = new OleDbConnectionStringBuilder();
String strExtendedProperties = String.Empty;
sbConnection.DataSource = filePath;
if (Path.GetExtension(filePath).Equals(".xls"))//for 97-03 Excel file
{
sbConnection.Provider = "Microsoft.Jet.OLEDB.4.0";
strExtendedProperties = "Excel 8.0;HDR=Yes;IMEX=1";//HDR=ColumnHeader,IMEX=InterMixed
}
else if (Path.GetExtension(filePath).Equals(".xlsx")) //for 2007 Excel file
{
sbConnection.Provider = "Microsoft.ACE.OLEDB.12.0";
strExtendedProperties = "Excel 12.0;HDR=Yes;IMEX=1";
}
sbConnection.Add("Extended Properties", strExtendedProperties);
List<string> listSheet = new List<string>();
using (OleDbConnection conn = new OleDbConnection(sbConnection.ToString()))
{
conn.Open();
DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach (DataRow drSheet in dtSheet.Rows)
{
if (drSheet["TABLE_NAME"].ToString().Contains("$"))//checks whether row contains '_xlnm#_FilterDatabase' or sheet name(i.e. sheet name always ends with $ sign)
{
listSheet.Add(drSheet["TABLE_NAME"].ToString());
}
}
}
return listSheet;
}
}