Xin chào sanjay8090,
Kiểm tra ví dụ này. Bây giờ hãy tham khảo và sửa mã của bạn
HTML
Id Name Country Id Name Country
không gian tên
C#
using System.Data; using System.Data.OleDb; using System.IO; using System.Web.Services;VB. Mạng lưới
Imports System.Data Imports System.Data.OleDb Imports System.IO Imports System.Web.ServicesMã số
C#
[WebMethod] public static string GetExcelData(string byteData) { byte[] bytes = Convert.FromBase64String(byteData); string filePath = HttpContext.Current.Server.MapPath("~/Files/Test.xls"); if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/Files"))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/Files")); } // Save file in File folder. File.WriteAllBytes(filePath, bytes); string extension = Path.GetExtension(filePath); string excelConnectionString = ""; switch (extension) { case ".xls": //Excel 97-03 excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'"; break; case ".xlsx": //Excel 07 excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'"; break; } excelConnectionString = String.Format(excelConnectionString, filePath); OleDbConnection excelConnection = new OleDbConnection(excelConnectionString); OleDbCommand cmdExcel = new OleDbCommand(); OleDbDataAdapter oleDA = new OleDbDataAdapter(); cmdExcel.Connection = excelConnection; excelConnection.Open(); DataTable dtExcelSchema; dtExcelSchema = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); excelConnection.Close(); excelConnection.Open(); cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; oleDA.SelectCommand = cmdExcel; DataSet ds = new DataSet(); oleDA.Fill(ds); excelConnection.Close(); // Delete saved file. Directory.Delete(HttpContext.Current.Server.MapPath("~/Files"), true); return ds.GetXml(); }VB. Mạng lưới
Public Shared Function GetExcelData(ByVal byteData As String) As String Dim bytes As Byte() = Convert.FromBase64String(byteData) Dim filePath As String = HttpContext.Current.Server.MapPath("~/Files/Test.xls") If Not Directory.Exists(HttpContext.Current.Server.MapPath("~/Files")) Then Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/Files")) End If ' Save file in File folder. File.WriteAllBytes(filePath, bytes) Dim extension As String = Path.GetExtension(filePath) Dim excelConnectionString As String = "" Select Case extension Case ".xls" 'Excel 97-03 excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'" Case ".xlsx" 'Excel 07 excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'" End Select excelConnectionString = String.Format(excelConnectionString, filePath) Dim excelConnection As OleDbConnection = New OleDbConnection(excelConnectionString) Dim cmdExcel As OleDbCommand = New OleDbCommand() Dim oleDA As OleDbDataAdapter = New OleDbDataAdapter() cmdExcel.Connection = excelConnection excelConnection.Open() Dim dtExcelSchema As DataTable dtExcelSchema = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing) Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString() excelConnection.Close() excelConnection.Open() cmdExcel.CommandText = "SELECT * From [" & SheetName & "]" oleDA.SelectCommand = cmdExcel Dim ds As DataSet = New DataSet() oleDA.Fill(ds) excelConnection.Close() ' Delete saved file. Directory.Delete(HttpContext.Current.Server.MapPath("~/Files"), True) Return ds.GetXml() End FunctionẢnh chụp màn hình
Tệp Excel
Biểu mẫu với dữ liệu Excel
Mẫu này sử dụng các tính năng CTP (Xem trước kỹ thuật cộng đồng). API và hành vi có thể thay đổi khi các tính năng này được phát hành với sự hỗ trợ đầy đủ
- Tải xuống tệp Excel mẫu này
- Nhấp vào nút Chọn tệp/Duyệt bên dưới và chọn tệp Excel mẫu hoặc tệp excel khác
- Chỉnh sửa nội dung excel hoặc tắt chỉnh sửa
- Tải xuống tệp excel
Chọn một tệp
Cho phép chỉnh sửa excel
Mẫu này được thiết kế cho kích thước màn hình lớn hơn
Trên thiết bị di động, hãy thử xoay màn hình của bạn, xem kích thước đầy đủ hoặc gửi email đến một thiết bị khác
Sử dụng API tệp, tệp Excel được đọc vào đối tượng Uint8Array, sau đó được chuyển sang phương thức tải do thư viện Excel đưa ra. Sau khi nhận được sổ làm việc và điều khiển Bảng tính được khởi tạo, sổ làm việc được đặt để hiển thị thông qua các tùy chọn của điều khiển. Sau đó, bạn có thể chỉnh sửa nội dung bảng tính và lưu lại thành tệp excel