【问题标题】:how to solve some excel data not showing in gridview如何解决一些excel数据没有显示在gridview中
【发布时间】:2014-05-23 09:24:55
【问题描述】:

我上传了 excel 文件,但 GridView 无法获取“U、V 和 AB”列的值,其他列可以。
“列 F21,F22,F28”中的值返回 null,因为这些列中有值。
我很感激有人可以帮助我~~~
Excel 文件:enter link description here

Excel 文件:

GridView

.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;
    }

【问题讨论】:

    标签: c# asp.net excel gridview


    【解决方案1】:

    我在 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>
    

    【讨论】:

    • 为您的代码提供一些解释,这样可以更容易地看到需要更改的内容。
    • 转到此链接它包含清晰的解释 (aspsnippets.com/Articles/…)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2020-09-04
    相关资源
    最近更新 更多