Làm cách nào để truy xuất dữ liệu từ excel bằng jquery?

Cơ sở Kiến thức này giải thích cách lưu và truy xuất dữ liệu Bảng tính dưới dạng một mảng byte trong Cơ sở dữ liệu

Dung dịch

Bạn có thể sử dụng các phương thức máy chủ “Open[]” và “Save[]” để đạt được yêu cầu này

Liên kết tài liệu API. https. //Cứu giúp. đồng bộ hóa. com/js/bảng tính/mở và lưu

Giải pháp JavaScript

[JS]

    

                                                                                               

           

[API web]

 
        string connectionString = ConfigurationManager.ConnectionStrings["FileData"].ConnectionString;
        [OperationContract]
        [WebGet[BodyStyle = WebMessageBodyStyle.Bare]]
        [System.Web.Http.ActionName["SaveFiletoDB"]]
        [AcceptVerbs["POST"]]
       // Save the Spreadsheet data as byte array to database
        public void SaveFiletoDB[]
        {
            string fileName = HttpContext.Current.Request.Params["fileName"], sheetModel = HttpContext.Current.Request.Params["sheetModel"], sheetData = HttpContext.Current.Request.Params["sheetData"];
            try
            {
                if [fileName.Length > 0]
                {
                    Stream dataStream = Spreadsheet.Save[sheetModel, sheetData, ExportFormat.XLSX, ExcelVersion.Excel2013];
                    dataStream.Position = 0;
                    Byte[] dataBytes = new BinaryReader[dataStream].ReadBytes[Convert.ToInt32[dataStream.Length]];
                    SqlConnection sqlCon = new SqlConnection[connectionString];
                    sqlCon.Open[];
                    SqlCommand sqlComm = new SqlCommand["INSERT INTO [dbo].[Table][[FileName], [FileData]] VALUES [@FileName, @FileData]", sqlCon];
                    sqlComm.Parameters.AddWithValue["@FileName", fileName];
                    sqlComm.Parameters.AddWithValue["@FileData", dataBytes];
                    sqlComm.ExecuteNonQuery[];
                    sqlCon.Close[];
                }
             }
            catch [Exception e]
            {
                // Error handling code here.
            }
         } 
 
        //Open Saved Spreadsheet data from database
        [OperationContract]
        [WebGet[BodyStyle = WebMessageBodyStyle.Bare]]
        [System.Web.Http.ActionName["OpenFileFromDB"]]
        [AcceptVerbs["POST"]]
        public string OpenFileFromDB[]
        {
           string filename = HttpContext.Current.Request.Params["filename"];
            ImportRequest importRequest = new ImportRequest[];
            SqlConnection sqlCon = new SqlConnection[connectionString];
            SqlCommand sqlComm = new SqlCommand["SELECT * FROM [dbo].[Table] WHERE [FileName] = '" + filename + "'", sqlCon];
            sqlCon.Open[];
            SqlDataReader sqlDR = sqlComm.ExecuteReader[];
            if [sqlDR.Read[]]
            {
                importRequest.FileStream = new MemoryStream[[byte[]]sqlDR.GetValue[1]];
            }
            sqlCon.Close[];
            return Spreadsheet.Open[importRequest];
        }
 

Giải pháp MVC

[CSHTML]

   

                                         @Html.DropDownList["importFileName", ViewBag.FileNameList as List]            

 

        @[Html.EJ[].Spreadsheet["Spreadsheet"]     .ScrollSettings[scroll =>     {         scroll.Height[550];     }]         ]        

[Controller]

        string connectionString = ConfigurationManager.ConnectionStrings["FileData"].ConnectionString;      
        // Save the Spreadsheet data as byte array to database.
        [AcceptVerbs[HttpVerbs.Post]]
        public void SaveFiletoDB[string fileName, string sheetModel, string sheetData]
        {
            try
            {
                if [fileName.Length > 0]
                {
                    Stream dataStream = Spreadsheet.Save[sheetModel, sheetData, ExportFormat.XLSX, ExcelVersion.Excel2013];
                    dataStream.Position = 0;
                    Byte[] dataBytes = new BinaryReader[dataStream].ReadBytes[Convert.ToInt32[dataStream.Length]];
                    SqlConnection sqlCon = new SqlConnection[connectionString];
                    sqlCon.Open[];
                    SqlCommand sqlComm = new SqlCommand["INSERT INTO [dbo].[Table][[FileName], [FileData]] VALUES [@FileName, @FileData]", sqlCon];
                    sqlComm.Parameters.AddWithValue["@FileName", fileName];
                    sqlComm.Parameters.AddWithValue["@FileData", dataBytes];
                    sqlComm.ExecuteNonQuery[];
                    sqlCon.Close[];
                }
            }
            catch [Exception e]
            {
                //Error handling code here.
            }
        } 
 
        //Open saved Spreadsheet data from database
        [AcceptVerbs[HttpVerbs.Post]]
        public string OpenFileFromDB[string filename]
        {
            ImportRequest importRequest = new ImportRequest[];
            SqlConnection sqlCon = new SqlConnection[connectionString];
            SqlCommand sqlComm = new SqlCommand["SELECT * FROM [dbo].[Table] WHERE [FileName] = '" + filename + "'", sqlCon];
            sqlCon.Open[];
            SqlDataReader sqlDR = sqlComm.ExecuteReader[];
            if [sqlDR.Read[]]
            {
                importRequest.FileStream = new MemoryStream[[byte[]]sqlDR.GetValue[1]];
            }
            sqlCon.Close[];
            return Spreadsheet.Open[importRequest];
        }
 

giải pháp ASP

[ASPX]

   

                                                    

                                                                                                                                                              

[C#]

        string connectionString = ConfigurationManager.ConnectionStrings["FileData"].ConnectionString;
        //Open saved Spreadsheet data from database
        [WebMethod]
        public static string OpenSpreadsheetFromDB[string filename]
        {
            ImportRequest importRequest = new ImportRequest[];
            SqlConnection sqlCon = new SqlConnection[connectionString];
            SqlCommand sqlComm = new SqlCommand["SELECT * FROM [dbo].[Table] WHERE [FileName] = '" + filename + "'", sqlCon];
            sqlCon.Open[];
            SqlDataReader sqlDR = sqlComm.ExecuteReader[];
            if [sqlDR.Read[]]
            {
                importRequest.FileStream = new MemoryStream[[byte[]]sqlDR.GetValue[1]];
            }
            sqlCon.Close[];
 
            return Spreadsheet.Open[importRequest];
        }
 
        // Save the Spreadsheet data as byte array to database.
        [WebMethod]
        public static void SaveSpreadsheetToDB[string fileName, string sheetModel, string sheetData]
        {
            try
            {
                if [fileName.Length > 0]
                {
                    MemoryStream fileStream = [MemoryStream]Spreadsheet.Save[sheetModel, sheetData, ExportFormat.XLSX, ExcelVersion.Excel2013];
                    Byte[] dataBytes = fileStream.ToArray[];
                    SqlConnection sqlCon = new SqlConnection[connectionString];
                    sqlCon.Open[];
                    SqlCommand sqlComm = new SqlCommand["INSERT INTO [dbo].[Table][[FileName], [FileData]] VALUES [@FileName, @FileData]", sqlCon];
                    sqlComm.Parameters.AddWithValue["@FileName", fileName];
                    sqlComm.Parameters.AddWithValue["@FileData", dataBytes];
                    sqlComm.ExecuteNonQuery[];
                    sqlCon.Close[];
                }
            }
            catch [Exception e]
            {
                // Error handling code here.
            }
        } 
 

Làm cách nào để đọc dữ liệu từ tệp Excel trong jQuery?

Đầu tiên, chúng ta sẽ tạo nút Tải tệp lên, sau đó là bảng HTML được ẩn trước và cuối cùng là nút Nhập liệu mà khi nhấp vào, sẽ gọi hàm để xuất dữ liệu Excel sang . Chạy trang sẽ như bên dưới. Bây giờ, chúng tôi tham khảo các tệp plugin jQuery "xlsx. cốt lõi. . Running the page will look like below. Now, we reference the jQuery plugin files "xlsx. core.

Làm cách nào để xuất dữ liệu từ Excel bằng jQuery?

Khi nhấp vào Nút Xuất, plugin jQuery table2excel được áp dụng cho Bảng HTML . Plugin jQuery table2excel chấp nhận tham số tên tệp đặt tên của tệp Excel. Ghi chú. Bạn sẽ nhận được thông báo Cảnh báo từ ứng dụng Microsoft Excel khi cố mở tệp Excel đã tạo.

Làm cách nào để lấy dữ liệu từ Excel trong JS?

Javascript Excel - Nhập dữ liệu từ Excel . Sau khi trang tính được tải vào đối tượng thư viện Excel, chúng ta có thể đọc từng giá trị ô và tạo một mảng JSON sẽ được sử dụng làm nguồn dữ liệu igGrid. The Excel file is read into Uint8Array object, which is then passed to the load method exposed by the Excel library. Once the worksheet is loaded into the Excel library object, we can read each cell value and build a JSON array that will be used as the igGrid data source.

Làm cách nào để hiển thị dữ liệu Excel trong bảng HTML bằng jQuery?

Trong ví dụ trên, chúng tôi đã tạo một ví dụ về cách nhập trang tính excel vào trang web HTML bằng jQuery. Với việc sử dụng nút HTML, chúng tôi có thể nhập trang tính excel vào tài liệu HTML . Chúng tôi cũng có thể thêm và xóa dữ liệu khỏi bảng HTML bằng cách sử dụng nút thêm và xóa.

Chủ Đề