如何解决一些不在gridview中显示的excel数据
我上传的是excel文件,但是GridView无法获得“U,V&AB”列的值,其他的列也没有。
“列F21,F22,F28”中的值由于这些列中存在值而返回null。
我感谢有人能帮助我~~~
Excel文件: 在这里input链接描述
Excel文件:
网格视图
.CS
protected void upload_Click(object sender, EventArgs e) { string path = Server.MapPath(@"~\Reference\"); string the_file = path + ExcelUpload.FileName; ExcelUpload.SaveAs(the_file); if (System.IO.File.Exists(the_file)) { DataTable test = getDataFromXLS(the_file); GridView1.DataSource = test; GridView1.DataBind(); foreach (DataRow dr in test.Rows) { if (dr["F31"].ToString() != "") { project_id.Value = dr["F31"].ToString(); if (dr["F21"].ToString() != "") rental.Value = dr["F21"].ToString(); if (dr["F22"].ToString() != "") ssf.Value = dr["F22"].ToString(); if (dr["F23"].ToString() != "") pct.Value = dr["F23"].ToString(); //rate if (dr["F24"].ToString() != "") { if (dr["F24"].ToString() == "I") rate.Value = "0"; else if (dr["F24"].ToString() == "E") rate.Value = "1"; } if (dr["F28"].ToString() != "") { DateTime date; var success = DateTime.TryParseExact(dr["F28"].ToString(), "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); startDate.Value = date.ToString("yyyy-MM-dd"); } Response.Write(startDate.Value); //null value } //end of if loop } //end of foreach } // end of file exist } private DataTable getDataFromXLS(string strFilePath) { try { string strConnectionString = ""; strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" + "Extended Properties=Excel 8.0;"; OleDbConnection cnCSV = new OleDbConnection(strConnectionString); cnCSV.Open(); OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [projection_n$]", cnCSV); OleDbDataAdapter daCSV = new OleDbDataAdapter(); daCSV.SelectCommand = cmdSelect; DataTable dtCSV = new DataTable(); daCSV.Fill(dtCSV); cnCSV.Close(); daCSV = null; return dtCSV; }
我发布了答案在vb.net你只是将其转换成C#。
//VB Code: Dim filePath As String = Server.MapPath(ExcelPath) Dim FileExt = Path.GetExtension(ExcelPath) '".xlsx" 'Open the connection with excel file based on excel version Dim con As OleDbConnection = Nothing If FileExt = ".xls" Then con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filePath & ";Extended Properties=Excel 8.0;") ElseIf FileExt = ".xlsx" Then con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties=Excel 12.0;") End If con.Open() 'Get the list of sheet available in excel sheet Dim dt As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing) Dim SheetCount As Integer = dt.Rows.Count 'Get first sheet name For value As Integer = 0 To SheetCount - 1 Dim getExcelSheetName As String = dt.Rows(value)("Table_Name").ToString() 'Select rows from first sheet in excel sheet and fill into dataset Dim ExcelCommand As New OleDbCommand("SELECT * FROM [" & getExcelSheetName & "]", con) Dim ExcelAdapter As New OleDbDataAdapter(ExcelCommand) 'Dim ExcelDataSet As New DataSet() Dim datTable As New DataTable() ExcelAdapter.Fill(datTable) ExcelDataSet.Tables.Add(datTable) Next con.Close() //End of VB Code //Grid Code: <asp:GridView ID="gvExcelSheetView" runat="server" ShowFooter="False" AllowPaging="false" CellPadding="3" HeaderStyle-BackColor="#ED1C24" BorderWidth="2" Style="width:1000px;" CellSpacing="0" Font-Names="Times New Roman" Font-Size="Small" HeaderStyle-Font-Names="Times New Roman" HeaderStyle-Font-Size="Medium" EditRowStyle-Font-Names="Times New Roman" EditRowStyle-Font-Size="Small" RowStyle-Font-Size="Small" RowStyle-Font-Names="Times New Roman" RowStyle-BackColor="White" AllowSorting="true" BorderColor="#3F5364"> <RowStyle BackColor="White" ForeColor="Black" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <HeaderStyle Font-Bold="True" ForeColor="White" /> <EmptyDataTemplate> <center> <span style="color: #ED1C24; font-weight: bold;width:1000px; text-align: center; font-size: medium;"> NO RECORDS FOUND</span></center> </EmptyDataTemplate> </asp:GridView>