Hi nauna,
The path of the excel file is mapped to OleDb using Server.MapPath method.
Since you are using online path as excel use below article and pass the stream.
Check this example. Now please take its reference and correct your code.
HTML
<asp:Button ID="btnImport" runat="server" Text="Import" OnClick="ImportExcel" />
<hr />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Namespaces
C#
using System.IO;
using System.Data;
using System.Net;
using ClosedXML.Excel;
VB.Net
Imports System.Data
Imports System.IO
Imports System.Net
Imports ClosedXML.Excel
Code
C#
protected void ImportExcel(object sender, EventArgs e)
{
string url = "https://file-examples-com.github.io/uploads/2017/02/file_example_XLSX_10.xlsx";
using (XLWorkbook workBook = new XLWorkbook(GetStreamFromUrl(url)))
{
IXLWorksheet workSheet = workBook.Worksheet(1);
DataTable dt = new DataTable();
bool firstRow = true;
foreach (IXLRow row in workSheet.Rows())
{
if (firstRow)
{
foreach (IXLCell cell in row.Cells())
{
dt.Columns.Add(cell.Value.ToString());
}
firstRow = false;
}
else
{
dt.Rows.Add();
int i = 0;
foreach (IXLCell cell in row.Cells())
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
i++;
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
private Stream GetStreamFromUrl(string url)
{
byte[] bytes = null;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
using (WebClient wc = new WebClient())
{
bytes = wc.DownloadData(url);
}
return new MemoryStream(bytes);
}
VB.Net
Protected Sub ImportExcel(sender As Object, e As EventArgs)
Dim url As String = "https://file-examples-com.github.io/uploads/2017/02/file_example_XLSX_10.xlsx"
Using workBook As New XLWorkbook(GetStreamFromUrl(url))
Dim workSheet As IXLWorksheet = workBook.Worksheet(1)
Dim dt As New DataTable()
Dim firstRow As Boolean = True
For Each row As IXLRow In workSheet.Rows()
If firstRow Then
For Each cell As IXLCell In row.Cells()
dt.Columns.Add(cell.Value.ToString())
Next
firstRow = False
Else
dt.Rows.Add()
Dim i As Integer = 0
For Each cell As IXLCell In row.Cells()
dt.Rows(dt.Rows.Count - 1)(i) = cell.Value.ToString()
i += 1
Next
End If
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Sub
Private Function GetStreamFromUrl(url As String) As Stream
Dim bytes As Byte() = Nothing
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Using wc As New WebClient()
bytes = wc.DownloadData(url)
End Using
Return New MemoryStream(bytes)
End Function
Screenshot