为什么我的Excel上传到数据库本地工作但不在Azure上工作?

时间:2018-01-18 16:48:22

标签: asp.net-mvc azure epplus

我在这里看过类似的帖子但没有运气。我的Excel上传在本地工作正常,但在Azure上它会收到一个非描述性错误(“处理您的请求时出错。)... EPPlus不能在Azure上运行吗?

我的控制器:

[HttpPost]
public ActionResult Index(HttpPostedFileBase upload)
{
    var fileName = Path.GetFileName(upload.FileName);
    var path = Path.Combine(Server.MapPath("~/Excel"), fileName);
    SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    if (upload.ContentLength > 0)
    {

        upload.SaveAs(path);
    }


    FileInfo excel = new FileInfo(path);
    using (var package = new ExcelPackage(excel))
    {
        var workbook = package.Workbook;
        var worksheet = workbook.Worksheets["Sheet1"];
        objConn.Open();

        int totalRows = worksheet.Dimension.End.Row;
        for (int i=2; i<=totalRows; i++)
        {
            string strSQL = "INSERT INTO Upload (Email, TimeStamp, EmployeeId, Name, Title, Department, Race, Gender, AnnualizedBase, AnnualizedTCC) VALUES (" 
                + " '" + System.Web.HttpContext.Current.User.Identity.GetUserId() + "', "
                + " '" + DateTime.Now + "', "
                + " '" + worksheet.Cells[i, 1].Text.ToString() + "', "
                + " '" + worksheet.Cells[i, 2].Text.ToString() + "', "
                + " '" + worksheet.Cells[i, 3].Text.ToString() + "', "
                + " '" + worksheet.Cells[i, 4].Text.ToString() + "', "
                + " '" + worksheet.Cells[i, 5].Text.ToString() + "', "
                + " '" + worksheet.Cells[i, 6].Text.ToString() + "', "
                + worksheet.Cells[i, 7].Value + ", "
                + worksheet.Cells[i, 8].Value 
                + ")";
            var objCmd = new SqlCommand(strSQL, objConn);
            objCmd.ExecuteNonQuery();
            ViewBag.Message = "Dataset Uploaded Successfully!";


        }
        objConn.Close();
        return View();
    }

2 个答案:

答案 0 :(得分:0)

我对Azure的了解不多。我使用过AWS主机。上传Excel时我遇到了同样的问题。

我已将AccessDatabaseEngine安装到我的EC2实例,它运行正常。

如果错误与ACCESS有关,请确保用户有权在硬盘上读/写文件/文件夹。

我希望这对你有所帮助。

答案 1 :(得分:0)

EPPlus也适用于Azure上下文。

由于您没有提供关于哪一行给您错误的确切信息,我只是猜测可能是您的数据库连接错误?您的连接字符串是否也适用于Azure环境?

您还可以尝试通过添加一些日志来调查错误来源。