C#VS2005将Pipe Delimited .TXT转换为Excel工作簿.XLS

我正在使用VS2005 C#和即时尝试将pipe道分隔文本文件转换为Excel工作簿格式。 以下是我的代码:

public partial class TextToExcel : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void SaveAsExcelBtn_Click(object sender, EventArgs e) { string xlExtension = ".csv"; string strExcelOutputFilename = "C:/Documents and Settings/rhlim/My Documents/" + DateTime.Now.ToString("yyyyMMddHHmmss") + xlExtension; // Before attempting to import the file, verify // that the FileUpload control contains a file. if (TextFile.HasFile) { // Get the name of the Excel spreadsheet. string strFileName = Server.HtmlEncode(TextFile.FileName); // Get the extension of the text. string strExtension = Path.GetExtension(strFileName); // Validate the file extension. if (strExtension != ".TXT" && strExtension!=".txt") { Response.Write("<script>alert('Failed to import. Cause: Invalid text file.');</script>"); return; } // Generate the file name to save the text file. //string strUploadFileName = "C:/Documents and Settings/rhlim/My Documents/Visual Studio 2005/WebSites/SoD/UploadFiles/" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension; using (StreamWriter outputWriter = new StreamWriter(File.Create(strExcelOutputFilename))) { StreamReader inputReader = new StreamReader(TextFile.FileContent); string fileContent = inputReader.ReadToEnd(); fileContent = fileContent.Replace('|', ';'); outputWriter.Write(fileContent); TextFile.SaveAs(strExcelOutputFilename); inputReader.Close(); } //string strExcelOutputFilename = "C:/Documents and Settings/rhlim/My Documents/" + DateTime.Now.ToString("yyyyMMddHHmmss")+xlExtension; // Save the Excel spreadsheet on server. //TextFile.SaveAs (strExcelOutputFilename); } else Response.Write("<script>alert('Failed to import. Cause: No file found');</script>"); } } 

目前我有一些文件保存错误

有什么build议么? 非常感谢!

在这里输入图像说明

这是因为Excel不支持pipe道,你必须将它转换为逗号或半列,如:

 using (StreamWriter outputWriter = new StreamWriter(File.Create(strExcelOutputFilename))) { StreamReader inputReader = new StreamReader(TextFile.FileContent); string fileContent = inputReader.ReadToEnd(); fileContent = fileContent.Replace('|', ','); outputWriter.Write(fileContent); } 

我GOOGLE了,希望它会帮助你: http : //csharp.net-informations.com/excel/csharp-create-excel.htm

或者,已经回答了: 从C#创buildExcel(.XLS和.XLSX)文件

在第一个链接,行xlWorkSheet.Cell [x,y]把元素放在专用单元格中。

仅供参考,xlsx格式(来自Office 2007的新版本)将为您提供具有良好操作能力的代码。

为了生成和处理excel文件,我个人比较喜欢NPOI库。 从Codeplex下载,将NPOI dll的引用添加到您的项目中。 将您想要的“模板”excel文件存储在已知位置,并使用您需要的任何列标题/格式。 然后你只需要使用npoi制作一个模板文件的副本,并在表格/行/列级别上进行操作,并放置任何你想要的数据。

示例代码片段看起来像这样。 假设你已经把你的input分成了一个string列表

 const string ExcelTemplateFile = "~/Resources/ExcelInputTemplate.xls"; const string ExcelWorksheetName = "Output Worksheet"; const int RequiredColumn = 1; private HSSFWorkbook CreateExcelWorkbook(IEnumerable<String> inputData) { FileStream fs = new FileStream(Server.MapPath(ExcelTemplateFile), FileMode.Open, FileAccess.Read); // Getting the complete workbook... HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true); // Getting the worksheet by its name... HSSFSheet sheet = templateWorkbook.GetSheet(ExcelWorksheetName); int startRowIterator = 1; foreach (string currentData in inputData) { sheet.CreateRow(startRowIterator).CreateCell(RequiredColumn).SetCellValue(currentData); } }