如何解决一些未在gridview中显示的excel数据

时间:2014-05-23 09:24:56

标签: c# asp.net excel gridview

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

Excel文件: enter image description here

GridView的
enter image description here

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

1 个答案:

答案 0 :(得分: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>