如何在asp.net中将此Excel工作表保存为文本文件(.txt)?

我想上传一个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."; } } 

我正在使用ASP.NET 4与C#,所以你能告诉我什么我应该能够将Excel表格保存为一个TXT文件,然后从中读取?

为了使Excel文件在文本编辑器中可读,必须将其转换为CSV文件格式。 这是因为.xlsx Excel文档(2007+)是复杂的XML层次结构。 如果您想知道什么是.xlsx文件的真正组成部分,请将其扩展名更改为.zip,然后将其解压缩。

因此,您将无法简单地将.xlsx文件的扩展名更改为.txt或.csv,并希望它在文本编辑器中可读。 您必须以这种格式从头开始保存文件。

在Excel中,将电子表格保存为.csv而不是.xlsx,然后您可以立即将其打开到文本编辑器中! 如果你真的想,你甚至可以把扩展名改为.txt。

如果您不告诉Excel将其自身保存为普通文本而不是其正常的XML结构,那么这些都不会成为问题。

如果你坚持支持.xlsx文件,有一种方法。 Office XML文件格式是一种开放的公共格式,允许您按自己的喜好操纵它。

您将需要:

  1. 下载Open XML SDK

  2. 仔细阅读文档

在你的情况下,你可能要访问特定的单元格值,读取它们的内容,然后将它们stream入一个新的文件。

上面的文档提供了以下代码片段来访问Excel文档中的单元格值:

 public static string XLGetCellValue(string fileName, string sheetName, string addressName) { const string worksheetSchema = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; const string sharedStringSchema = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; string cellValue = null; // Retrieve the stream containing the requested // worksheet's info. using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(fileName, false)) { // Get the main document part (workbook.xml). XmlDocument doc = new XmlDocument(); doc.Load(xlDoc.WorkbookPart.GetStream()); // Create a namespace manager, so you can search. // Add a prefix (d) for the default namespace. NameTable nt = new NameTable(); XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); nsManager.AddNamespace("d", worksheetSchema); nsManager.AddNamespace("s", sharedStringSchema); string searchString = string.Format("//d:sheet[@name='{0}']", sheetName); XmlNode sheetNode = doc.SelectSingleNode(searchString, nsManager); if (sheetNode != null) { // Get the relId attribute. XmlAttribute relationAttribute = sheetNode.Attributes["r:id"]; if (relationAttribute != null) { string relId = relationAttribute.Value; // Load the contents of the workbook. XmlDocument sheetDoc = new XmlDocument(nt); sheetDoc.Load(xlDoc.WorkbookPart.GetPartById(relId).GetStream()); XmlNode cellNode = sheetDoc.SelectSingleNode(string.Format("//d:sheetData/d:row/d:c[@r='{0}']", addressName), nsManager); if (cellNode != null) { XmlAttribute typeAttr = cellNode.Attributes["t"]; string cellType = string.Empty; if (typeAttr != null) { cellType = typeAttr.Value; } XmlNode valueNode = cellNode.SelectSingleNode("d:v", nsManager); if (valueNode != null) { cellValue = valueNode.InnerText; } if (cellType == "b") { if (cellValue == "1") { cellValue = "TRUE"; } else { cellValue = "FALSE"; } } else if (cellType == "s") { if (xlDoc.WorkbookPart.SharedStringTablePart != null) { XmlDocument stringDoc = new XmlDocument(nt); stringDoc.Load(xlDoc.WorkbookPart.SharedStringTablePart.GetStream()); // Add the string schema to the namespace manager. nsManager.AddNamespace("s", sharedStringSchema); int requestedString = Convert.ToInt32(cellValue); string strSearch = string.Format("//s:sst/s:si[{0}]", requestedString + 1); XmlNode stringNode = stringDoc.SelectSingleNode(strSearch, nsManager); if (stringNode != null) { cellValue = stringNode.InnerText; } } } } } } } return cellValue; } 

从那里,你可以做任何你喜欢的单元格值=)

您不能以文本格式保存Excel文件,您需要使用.csv扩展名而不是使用xlsx或xls,并将其另存为.txt