我正在尝试导入和导出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()
现在我可以下载Excel但是当我上传并尝试阅读时,它给了我“外部表不是预期的格式”错误但是当我手动创建任何excel时并尝试上传和阅读该Excel,然后它工作正常,不会给出任何错误
阅读EXCEL的代码
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();
如果您在不使用任何第三方DLL的情况下向我提供答案并且生产服务器没有安装办公室并且将来不会安装它将会很好。所以我也不能使用interlope
当我打开下载的excel并将其保存为“EXCEL 97-2003工作簿”时,这个新的excel工作正常
答案 0 :(得分:0)
您是否只能导出表格标签并重试?
如果你手动填写Excel,它肯定是正确的格式,而导出的html可以解释不正确。
我建议使用ClosedXml(免费和开源),即使你要求不建议第三方dll