Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从ASHX页面读取excel文件?_Javascript_Asp.net_Filehandler - Fatal编程技术网

Javascript 如何从ASHX页面读取excel文件?

Javascript 如何从ASHX页面读取excel文件?,javascript,asp.net,filehandler,Javascript,Asp.net,Filehandler,您好,我已经上传文件到一个亚马逊s3服务器,我如何读取excel文件,并希望发送excel数据到数据库。 我的代码是 <script type="text/javascript"> var obj = null; $(function () { $('#fileupload').fileupload({ replaceFileInput: false, f

您好,我已经上传文件到一个亚马逊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>

var obj=null;
$(函数(){
$('#fileupload')。fileupload({
replaceFileInput:false,
formData:函数(表单){
返回[{name:“name1”,value:“value1”},{name:“name2”,value:“value2”}];
$('#btnGo')。单击(函数(){
obj.submit();
});
});
还有我的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
    }
公共类AjaxFileHandler:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
var file=context.Request.Files[0];
字符串fileName=fileName=Guid.NewGuid().ToString()+file.fileName;
streamContentFile=context.Request.Files[0]。InputStream;
var iFileSize=context.Request.Files[0].ContentLength;
字节[]数据=新字节[iFileSize];
int bytes_read=0;
while(字节\u读取
您需要两件事:

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

  • 我使用的驱动程序,必须安装在服务器上
  • 该文件位于
    App\u Data
    文件夹中(在您的情况下,我想该文件应该放在公共库中)
  • 该查询是一个
    UPDATE
    查询;您可以使用公共DB代码段替换为
    SELECT
    INSERT
    查询

    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();
        }
    

我会使用excel(xslx)或(xls)的开放源码库。它们非常容易使用,我正在使用EPPlus进行大量excel导入/导出,并且工作得很好。这些库没有外部依赖性,您可以在服务器端使用。

我也收到了错误信息Microsoft Office Access数据库引擎无法打开或写入文件“”。该文件已被其他用户以独占方式打开,或者您需要查看和写入其数据的权限。“@user1527989应用程序需要对该文件进行写入授权。通过谷歌搜索,您可以找到许多解决方案。无论如何,如果AmanZonsBucket有太多权限限制,您可以在Application\App\u数据文件夹中编辑该文件,然后将其发送到Amazon