我有两个按钮(LoadFile和ImportFile)。我想从directy单击加载文件和浏览器获取Excel文件,然后单击“导入”按钮,以便将数据加载到数据库中的表中。 Excel表格被称为" BankStatement"超过1000行。我之前没有做过,我正在使用Linq to SQL。
这是我的编码,但我不确定如何继续或获取方法。有人可以帮帮我吗。感谢
装载
public void LoadExcel()
{
string _LoadPath = @"C:\NewFNBFile1.xls";
TextBox1.Text = _LoadPath.ToString();
var _File = new ExcelQueryFactory(_LoadPath);
_File.AddMapping("Number", "Number");
_File.AddMapping("DateReceived", "DateReceived");
_File.AddMapping("Description1", "Description1"
_File.AddMapping("Description2", "Description2");
_File.AddMapping("Description3", "Description3");
_File.AddMapping("Amount", "Amount");
_File.AddMapping("Balance", "Balance");
_File.AddMapping("AccruedCharges", "AccruedCharges");
}
protected void btnBroswer_Click(object sender, EventArgs e)
{
LoadExcel();
}
导入
protected void btnImport_Click(object sender, EventArgs e)
{
var _list = new ExcelQueryFactory(@"C:\NewFNBFile1.xls");
var _Show = from x in _dc.TESTING_NewBankFile1_s
where x ["Description3"] == "A19C28425645285"
select new
{
Number = x["Number"],
DateReceived = x["DateReceived"],
Description1 = x["Description1"],
Description2 = x["Description2"],
Description3 = x["Description3"],
Amount = x["Amount"],
Balance = x["Balance"],
AccruedCharges = x["AccruedCharges"],
};
}
答案 0 :(得分:0)
请参阅以下c#代码
public System.Data.DataTable GetTable(string filename, string SheetName, string outTableName)
{
OleDbConnection oleConn = null;
OleDbDataAdapter oleAdapter = null;
try
{
string Con = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source=" + filename + ";" +
@"Extended Properties=" + Convert.ToChar(34).ToString() +
@"Excel 12.0;" + "Imex=1;" + "HDR=Yes;" + Convert.ToChar(34).ToString() ;
oleConn = new OleDbConnection(Con);
oleConn.Open();
OleDbCommand oleCmdSelect = new OleDbCommand();
oleCmdSelect = new OleDbCommand(
@"SELECT * FROM ["
+ SheetName
+ "$" + "]", oleConn);
oleAdapter = new OleDbDataAdapter();
oleAdapter.SelectCommand = oleCmdSelect;
System.Data.DataTable dt = new System.Data.DataTable(outTableName);
oleAdapter.FillSchema(dt, SchemaType.Source);
oleAdapter.Fill(dt);
oleCmdSelect.Dispose();
oleCmdSelect = null;
oleAdapter.Dispose();
oleAdapter = null;
oleConn.Dispose();
oleConn = null;
return dt;
}
}