I have created sample code that will help you.
HTML
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Has Header ?" />
<asp:RadioButtonList ID="rbHDR" runat="server">
<asp:ListItem Text="Yes" Value="Yes" Selected="True">
</asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
</asp:RadioButtonList>
<asp:Panel ID="pnlContents" runat="server">
<asp:GridView ID="gvReadefile" runat="server" Visible="false">
</asp:GridView>
<asp:Label ID="lblText" runat="server" Visible="false" />
</asp:Panel>
<br />
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="return PrintPanel();" />
</form>
<script type="text/javascript">
function PrintPanel() {
var panel = document.getElementById("<%=pnlContents.ClientID %>");
var printWindow = window.open('', '', 'height=400,width=800');
printWindow.document.write('<html><head><title>DIV Contents</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(panel.innerHTML);
printWindow.document.write('</body></html>');
printWindow.document.close();
setTimeout(function () {
printWindow.print();
}, 500);
return false;
}
</script>
</body>
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FilePath = Server.MapPath(FolderPath + fileName);
FileUpload1.SaveAs(FilePath);
string conStr = "";
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
BindGrid(FilePath, rbHDR.SelectedItem.Text, conStr);
break;
case ".xlsx": //Excel 07
conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
BindGrid(FilePath, rbHDR.SelectedItem.Text, conStr);
break;
case ".txt":
lblText.Visible = true;
StreamReader objStreamReader = default(StreamReader);
objStreamReader = File.OpenText(FilePath);
lblText.Text = objStreamReader.ReadToEnd();
break;
default:
break;
}
}
}
private void BindGrid(string FilePath, string isHDR, string conStr)
{
gvReadefile.Visible = true;
conStr = String.Format(conStr, FilePath, isHDR);
OleDbConnection connExcel = new OleDbConnection(conStr);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();
gvReadefile.Caption = Path.GetFileName(FilePath);
gvReadefile.DataSource = dt;
gvReadefile.DataBind();
}
VB
Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
If FileUpload1.HasFile Then
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
Dim FilePath As String = Server.MapPath(FolderPath & fileName)
FileUpload1.SaveAs(FilePath)
Dim conStr As String = ""
Select Case Extension
Case ".xls"
'Excel 97-03
conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
BindGrid(FilePath, rbHDR.SelectedItem.Text, conStr)
Exit Select
Case ".xlsx"
'Excel 07
conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
BindGrid(FilePath, rbHDR.SelectedItem.Text, conStr)
Exit Select
Case ".txt"
lblText.Visible = True
Dim objStreamReader As StreamReader = Nothing
objStreamReader = File.OpenText(FilePath)
lblText.Text = objStreamReader.ReadToEnd()
Exit Select
Case Else
Exit Select
End Select
End If
End Sub
Private Sub BindGrid(FilePath As String, isHDR As String, conStr As String)
gvReadefile.Visible = True
conStr = [String].Format(conStr, FilePath, isHDR)
Dim connExcel As New OleDbConnection(conStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
Dim dt As New DataTable()
cmdExcel.Connection = connExcel
connExcel.Open()
Dim dtExcelSchema As DataTable
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
connExcel.Close()
connExcel.Open()
cmdExcel.CommandText = (Convert.ToString("SELECT * From [") & SheetName) + "]"
oda.SelectCommand = cmdExcel
oda.Fill(dt)
connExcel.Close()
gvReadefile.Caption = Path.GetFileName(FilePath)
gvReadefile.DataSource = dt
gvReadefile.DataBind()
End Sub
I hope this code help you.