我知道这个问题是重复的,但问题是我我都试过几乎所有的链路解决方案,我没有获得成功。因此,这里的情景: 我有一个输入类型=文件和放大器;当我点击提交按钮,它发送的文件。
这里是code:
[HttpPost]
公众的ActionResult指数(HttpPostedFileBase excel_File)
{
如果(excel_File!= NULL)
{
//保存上传的文件到光盘上。
字符串saved_FileName = Path.Combine(@ ConfigurationManager.AppSettings [excel_file_storage_path] + excel_File.FileName); //从拍摄的web.config文件上传的位置
excel_File.SaveAs(saved_FileName);
VAR的connectionString =的String.Format(供应商= Microsoft.ACE.OLEDB.12.0;数据源= {0};扩展属性= Excel的12.0的Xml; HDR = YES,saved_FileName);
//装满从Sheet1工作表信息的数据集。
VAR适配器=新OleDbDataAdapter的(SELECT * FROM [表Sheet1 $]的connectionString);
变种DS =新的DataSet();
adapter.Fill(DS); //我在这里得到错误。
数据表数据= ds.Tables [0];
VAR obj_Table =新的DataTable(Sample_Upload_Expense);
obj_Table.Columns.Add(ID的typeof(Int16的));
obj_Table.Columns.Add(姓名,typeof运算(字符串));
的for(int i = 0; I< data.Rows.Count - 1;我++)
{
obj_Table.Rows.Add(data.Rows [I]技术领域<双>(ID),data.Rows [I]技术领域<字符串>(名称));
}
VAR arrParam =新的名单,其中,SqlParameter的>();
VAR对=新的SqlParameter();
para.ParameterName =@RowList;
para.SqlDbType = System.Data.SqlDbType.Structured;
para.Value = obj_Table;
VAR pNumRowsChanged =新的SqlParameter(@ NumRowsChanged,SqlDbType.Int);
pNumRowsChanged.Value = 0;
arrParam.Add(对位);
arrParam.Add(pNumRowsChanged);
字符串PROC =SPInsertExpense;
int无= DataProvider.ProcRetunAffectedRows(PROC,arrParam);
返回查看();
}
帮助我。我被困2天,我没能找到解决办法。
在web.config文件路径:
<的appSettings>
<添加键=excel_file_storage_path值=D:\ webapps中\上传\ UploadedExcelDocuments \/>
< /的appSettings>
我曾尝试:
在我安装的2007 Office系统驱动程序 - 数据连接组件来解决,如果有任何连接问题,但它不工作 在试图把各地的数据源的单引号。 在我把静态路径连接字符串,但也不要工作。 此外这个的,但它不是我的问题有关。 解决方案最后,它的工作。我想这,和放大器;有效。现状在连接字符串,它需要两个 (分号)结尾。所以,我的的connectionString 必须像如下:
VAR的connectionString =的String.Format(供应商= Microsoft.ACE.OLEDB.12.0;数据源= {0};扩展属性='Excel的12.0的Xml; HDR = YES; ;,saved_FileName);
I know this question is repeated but problem with me is I have tried almost all the link for solution and I didnt get the success. So here is the scenario: I have a input type = file & when I click on submit button, it POST the file.
here is the code:
[HttpPost]
public ActionResult Index(HttpPostedFileBase excel_File)
{
if (excel_File != null)
{
//Save the uploaded file to the disc.
string saved_FileName = Path.Combine(@ConfigurationManager.AppSettings["excel_file_storage_path"] + excel_File.FileName); // Taking file upload location from web.config
excel_File.SaveAs(saved_FileName);
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0 Xml;HDR=YES", saved_FileName);
//Fill the dataset with information from the Sheet1 worksheet.
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds); // I get error here.
DataTable data = ds.Tables[0];
var obj_Table = new DataTable("Sample_Upload_Expense");
obj_Table.Columns.Add("Id", typeof(Int16));
obj_Table.Columns.Add("Name", typeof(string));
for (int i = 0; i < data.Rows.Count - 1; i++)
{
obj_Table.Rows.Add(data.Rows[i].Field<double>("Id"), data.Rows[i].Field<string>("Name"));
}
var arrParam = new List<SqlParameter>();
var para = new SqlParameter();
para.ParameterName = "@RowList";
para.SqlDbType = System.Data.SqlDbType.Structured;
para.Value = obj_Table;
var pNumRowsChanged = new SqlParameter("@NumRowsChanged", SqlDbType.Int);
pNumRowsChanged.Value = 0;
arrParam.Add(para);
arrParam.Add(pNumRowsChanged);
string proc = "SPInsertExpense";
int no = DataProvider.ProcRetunAffectedRows(proc, arrParam);
return View();
}
Help me. I am stuck from 2 days and I am not able to find the solution.
file path in web.config:
<appSettings>
<add key="excel_file_storage_path" value="D:\webapps\uploads\UploadedExcelDocuments\" />
</appSettings>
I have tried:
I installed 2007 Office System Driver - Data Connectivity Components to solve if there is any connectivity issue but it doesnt work. Tried putting single quotes around the data source. I put static path in connection string but that also dont work. Also this, though it is not related with my issue.解决方案
Finally it worked. I tried this, & it worked. Actuality in connection string, it needs two ; (semicolons) at the end. So my connectionString must be like follow:
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;';", saved_FileName);