单击时我想获取标题列名称。 我将gridview与自动生成的列一起使用,因为我不知道我随应用程序上传的excel文件的结构。
我试图将预定义的排序功能与sort ='true'属性一起使用,但这并不能解决我的问题。
<div>
Import Excel File:
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
<br />
<br />
<asp:Label ID="MessageLabel" runat="server"></asp:Label>
<br />
<br />
<div style="width: 50%; height: 250px; overflow: scroll">
<asp:GridView ID="gvExcelFile" runat="server" CellPadding="4" ForeColor="Black" Width="750px" AllowSorting="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellSpacing="2" Height="256px">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="Gray" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"></asp:ObjectDataSource>
</div>
</div>
protected void btnUpload_Click(object sender, EventArgs e)
{
string connectionString = "";
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
FileUpload1.SaveAs(fileLocation);
if (fileExtension == ".xls")
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT top 20 * FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dtExcelRecords.Clear();
dAdapter.Fill(dtExcelRecords);
gvExcelFile.DataSource = dtExcelRecords;
gvExcelFile.DataBind();
con.Close();
}
}
我希望允许用户选择列以进行进一步的文件处理。
例如,如果单击某些列标题,则将该列名称附加在列表中,然后将其另存为某个数组中的数据,以便在数据库中进行进一步处理。