简体   繁体   中英

How to read excel File from ASHX page?

Hi i have uploaded file to one amazon s3 server,how can i read excel file and want to send excel data to database. my code is

<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>

And my ashx page, where i need to read excel data

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
    }

you need two things:

  • a Driver that allows code to read Excel content
  • access to this file
  • a query over excel data

In this sample:

  • I use ACE (Microsoft Access Database Engine) driver, that must be installed on the server
  • the file is in App_Data folder (in your case the file should be rached CommonBLL library, i suppose)
  • the query is an UPDATE query; you can replace with a SELECT or INSERT query, using common DB SNippets.

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

I would use open source libraries for excel, EPPlus (xslx) or NPOI (xls). They are very easy to use, and I am using EPPlus for numerous excel imports / exports and it's working great. These libraries have no external dependancies, and you can use it on server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM