【问题标题】:How to read excel File from ASHX page?如何从 ASHX 页面读取 excel 文件?
【发布时间】:2012-07-23 08:34:24
【问题描述】:

您好,我已将文件上传到一台 amazon s3 服务器,我如何读取 excel 文件并希望将 excel 数据发送到数据库。 我的代码是

<script type="text/javascript">
         var obj = null;
         $(function () {
             $('#fileupload').fileupload({
                 replaceFileInput: false,
                 formData: function (form) {
                     return [{ name: "name1", value: "value1" }, { name: "name2", value: "value2"}];
             $('#btnGo').click(function () {
                 obj.submit();
             });
         });
     </script>

还有我的ashx页面,我需要读取excel数据的地方

public class AjaxFileHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var file = context.Request.Files[0];
            string fileName=fileName = Guid.NewGuid().ToString() + file.FileName;
            Stream streamContentFile = context.Request.Files[0].InputStream;

            var iFileSize = context.Request.Files[0].ContentLength;
            byte[] data = new byte[iFileSize];
            int bytes_read = 0;
            while (bytes_read < iFileSize)
            {
                int bytes_read_this_iteration = streamContentFile.Read(data, bytes_read, iFileSize - bytes_read);
            streamContentFile.Close();
            streamContentFile.Dispose();
            CommonBLL.UploadTemporaryFilesToS3Bucket(fileName, data);
//Here i need to read excel code can you provide how to do that pleas
    }

【问题讨论】:

    标签: javascript asp.net filehandler


    【解决方案1】:

    我会为 excel 使用开源库,EPPlus (xslx) 或 NPOI (xls)。它们非常易于使用,我正在使用 EPPlus 进行大量的 excel 导入/导出,并且效果很好。这些库没有外部依赖,可以在服务器端使用。

    【讨论】:

      【解决方案2】:

      你需要两件事:

      • 允许代码读取 Excel 内容的驱动程序
      • 访问此文件
      • 对excel数据的查询

      在此示例中:

      • 我使用ACE (Microsoft Access Database Engine)驱动,必须安装在服务器上
      • 该文件位于App_Data 文件夹中(在您的情况下,我想该文件应该是CommonBLL 库)
      • 查询是UPDATE 查询;您可以使用常见的 DB SNippets 替换为 SELECTINSERT 查询。

        string fileName= Server.MapPath( "~/App_Data/MyFile.xls");
        string sheetName= "Sheet1";
        string connString = string.Format(
              "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No'"
              , fileName);
        string command = string.Format("UPDATE [{0}${1}:{1}] SET F1='{2}'", sheetName,cellName, cellValue);
        
           using (OleDbConnection oledbConn = new OleDbConnection(connString))
            {
                oledbConn.Open();
                using (OleDbCommand cmd = new OleDbCommand(command, oledbConn))
                    cmd.ExecuteNonQuery();
        
                oledbConn.Close();
            }
        

      【讨论】:

      • 我收到错误消息,因为“Microsoft Office Access 数据库引擎无法打开或写入文件''。它已被其他用户以独占方式打开,或者您需要查看和写入其数据的权限。 "
      • @user1527989 应用程序需要对该文件的写入授权。谷歌搜索你可以找到很多解决方案。无论如何,如果 AmanzonSrBucket 的权限限制太多,您可以编辑 Application\App_Data 文件夹中的文件,然后将其发送到亚马逊
      猜你喜欢
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多