我正在尝试上传一个 excel 表并将其保存为文本文件,然后从该文本文件中读取。我的一个朋友在他的应用程序中实现了这样的功能,并且运行良好。我只是复制了他的代码,但它不能正常工作。它将 excel 工作表保存为文本文件,但是当我打开文本文件时,我发现数据已损坏,并且有很多 Unicode 或奇怪的符号以及许多不必要的行,例如:
; þÿÿÿ þÿÿÿ :
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ASP.NET 代码:
<asp:FileUpload ID="Upload" runat="server" />
<asp:Button ID="btn_upload" runat="server" Text="Upload" OnClick="UploadButton_Click" />
<asp:Label ID="Label1" runat="server" />
C#代码:
protected void UploadButton_Click(object sender, EventArgs e)
{
if (Upload.HasFile)
{
try
{
Upload.SaveAs(Server.MapPath("~/Files/Test_" + DateTime.Now.Year + "_" + DateTime.Now.Month + ".txt"));
LabelUpload.Text = "Upload File Name: " + Upload.PostedFile.FileName + "<br>" + "Type: " + Upload.PostedFile.ContentType + " File Size: " + Upload.PostedFile.ContentLength + " kb<br>";
string filename = Server.MapPath("~/Files/Test_" + DateTime.Now.Year + "_" + DateTime.Now.Month + ".txt");
if (System.IO.File.Exists(filename))
{
LabelUpload.Text = LabelUpload.Text + "Uploaded Successfully";
}
}
catch (Exception ex)
{
Label1.Text = "Error: " + ex.Message.ToString();
}
}
else
{
LabelUpload.Text = "Please select a file to upload.";
}
}
我正在使用带有 C# 的 ASP.NET 4,所以你能告诉我应该怎样才能将 Excel 工作表保存为 txt 文件然后从中读取吗?