C# 外部表的格式不是预期的asp.net

C# 外部表的格式不是预期的asp.net,c#,asp.net,vb.net,excel,C#,Asp.net,Vb.net,Excel,我正在尝试导入和导出Excel。下面是流程图。。一个用户下载Excel,另一个用户上传。Excel从一个web应用程序下载,然后上载到另一个web应用程序。(其中选择Excel数据并执行不同的操作) 下载EXCEL的代码 HttpContext.Current.Response.Clear() HttpContext.Current.Response.ClearContent() HttpContext.Current.Response.ClearHeaders(

我正在尝试导入和导出Excel。下面是流程图。。一个用户下载Excel,另一个用户上传。Excel从一个web应用程序下载,然后上载到另一个web应用程序。(其中选择Excel数据并执行不同的操作)

下载EXCEL的代码

HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.ClearContent()
        HttpContext.Current.Response.ClearHeaders()
        HttpContext.Current.Response.Buffer = True
        HttpContext.Current.Response.ContentType = "application/ms-excel"
        HttpContext.Current.Response.Write("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">")
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls")

        HttpContext.Current.Response.Charset = "utf-8"
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250")
        'sets font
        HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>")
        HttpContext.Current.Response.Write("<BR><BR><BR>")
        'sets the table border, cell spacing, border color, font of the text, background, foreground, font height
        HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " + "borderColor='#000000' cellSpacing='0' cellPadding='0' " + "style='font-size:10.0pt; font-family:Calibri; background:white;'>")
        'am getting my grid's column headers
        Dim columnscount As Integer = GridView_Result.Columns.Count

        Dim Console As String = ""
        HttpContext.Current.Response.Write("<TR>")
        For Each column In dtEmp.Columns
            HttpContext.Current.Response.Write("<Td>")
            HttpContext.Current.Response.Write(column.ColumnName)
            HttpContext.Current.Response.Write("</Td>")
        Next
        HttpContext.Current.Response.Write("</TR>")
        For Each row As DataRow In dtEmp.Rows
            'write in new row
            HttpContext.Current.Response.Write("<TR style=background-color :#FFFFFF>")
            For i As Integer = 0 To dtEmp.Columns.Count - 1
                HttpContext.Current.Response.Write("<Td>")
                HttpContext.Current.Response.Write(row(i).ToString())
                HttpContext.Current.Response.Write("</Td>")
            Next

            HttpContext.Current.Response.Write("</TR>")
        Next
        HttpContext.Current.Response.Write("</Table>")
        HttpContext.Current.Response.Write("</font>")
        HttpContext.Current.Response.Flush()
        HttpContext.Current.Response.[End]()

        Response.ClearContent()
如果您提供的答案不使用任何第三方DLL,并且生产服务器没有安装office,并且将来也不会安装,这将非常好。所以我也不能使用interlope


当我打开下载的excel并将其保存为“excel 97-2003工作簿”时,此新excel工作正常

您能否仅导出表标记并重试

如果手动填写Excel,它的格式肯定是正确的,而导出的html可能解释不正确

我建议使用ClosedXml(免费和开源),即使你不建议使用第三方dll

var FilePATH = Server.MapPath("~/Reports/" + "DEMO.xls");
            if (FileUpload1.HasFile)
            {
                string con = "provider=Microsoft.ACE.OLEDB.12.0;;Data Source={0};" +
                             "Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";

                FileUpload1.SaveAs(FilePATH);



                con = String.Format(con, FilePATH);
                OleDbConnection connExcel = new OleDbConnection(con);
                OleDbCommand cmdExcel = new OleDbCommand();
                OleDbDataAdapter oda = new OleDbDataAdapter();
                DataTable dt = new DataTable();
                cmdExcel.Connection = connExcel;

                //Get the name of First Sheet
                connExcel.Open();
                DataTable dtExcelSchema;
                dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                connExcel.Close();

                //Read Data from First Sheet
                connExcel.Open();
                cmdExcel.CommandText = "SELECT * From  [" + SheetName + "]";
                oda.SelectCommand = cmdExcel;
                oda.Fill(dt);
                connExcel.Close();