【发布时间】:2016-07-04 22:12:53
【问题描述】:
我正在尝试导入和导出 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 的情况下为我提供答案并且生产服务器没有安装 office 并且将来不会安装它会很好。所以我也不能使用 interlope
当我打开下载的 excel 并将其保存为“EXCEL 97-2003 工作簿”时,这个新的 excel 工作正常
【问题讨论】: