C#VS2005将管道分隔.TXT转换为Excel工作簿.XLS
我正在使用VS2005 C#,我正在尝试将管道分隔的文本文件转换为excel工作簿格式。下面是我的代码:C#VS2005将管道分隔.TXT转换为Excel工作簿.XLS,c#,visual-studio,excel,visual-studio-2005,C#,Visual Studio,Excel,Visual Studio 2005,我正在使用VS2005 C#,我正在尝试将管道分隔的文本文件转换为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)
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>");
}
}
public分部类TextToExcel:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效SaveAsExcelBtn\u单击(对象发送方,事件参数e)
{
字符串xlExtension=“.csv”;
字符串strExcelOutputFilename=“C:/Documents and Settings/rhlim/My Documents/”+DateTime.Now.ToString(“yyyyMMddHHmmss”)+xlExtension;
//在尝试导入文件之前,请验证
//FileUpload控件包含一个文件。
if(TextFile.HasFile)
{
//获取Excel电子表格的名称。
字符串strFileName=Server.HtmlEncode(TextFile.FileName);
//获取文本的扩展名。
字符串strExtension=Path.GetExtension(strFileName);
//验证文件扩展名。
if(strExtension!=“.TXT”&&strExtension!=“.TXT”)
{
Response.Write(“警报('导入失败。原因:无效文本文件');”;
返回;
}
//生成文件名以保存文本文件。
//string strUploadFileName=“C:/Documents and Settings/rhlim/My Documents/visualstudio 2005/WebSites/SoD/UploadFiles/”+DateTime.Now.ToString(“yyyyMMddHHmmss”)+strExtension;
使用(StreamWriter outputWriter=new StreamWriter(File.Create(strExcelOutputFilename)))
{
StreamReader inputReader=新的StreamReader(TextFile.FileContent);
字符串fileContent=inputReader.ReadToEnd();
fileContent=fileContent.Replace(“|”和“;”);
outputWriter.Write(文件内容);
TextFile.SaveAs(strExcelOutputFilename);
inputReader.Close();
}
//字符串strExcelOutputFilename=“C:/Documents and Settings/rhlim/My Documents/”+DateTime.Now.ToString(“yyyyMMddHHmmss”)+xlExtension;
//将Excel电子表格保存到服务器上。
//TextFile.SaveAs(strExcelOutputFilename);
}
else Response.Write(“警报(‘导入失败。原因:未找到文件’);”;
}
}
目前我有一些文件保存错误
有什么建议吗?非常感谢
我在谷歌上搜索了一下,希望它能帮助你: 或者,已经回答: 在第一个链接中,行xlsheetwork.Cell[x,y]将元素放入专用单元格中
仅供参考,xlsx格式(Office 2007新增)将为您提供强大的代码处理能力。我在谷歌上搜索过,希望它能帮助您: 或者,已经回答: 在第一个链接中,行xlsheetwork.Cell[x,y]将元素放入专用单元格中
仅供参考,xlsx格式(Office 2007新增)将为您提供强大的代码处理能力。这是因为Excel不支持管道,您必须将其转换为逗号或半列,如:
using (StreamWriter outputWriter = new StreamWriter(File.Create(strExcelOutputFilename)))
{
StreamReader inputReader = new StreamReader(TextFile.FileContent);
string fileContent = inputReader.ReadToEnd();
fileContent = fileContent.Replace('|', ',');
outputWriter.Write(fileContent);
}
这是因为Excel不支持管道,您必须将其转换为逗号或半列,如:
using (StreamWriter outputWriter = new StreamWriter(File.Create(strExcelOutputFilename)))
{
StreamReader inputReader = new StreamReader(TextFile.FileContent);
string fileContent = inputReader.ReadToEnd();
fileContent = fileContent.Replace('|', ',');
outputWriter.Write(fileContent);
}
对于生成和操作excel文件,我个人更喜欢库。从Codeplex下载,将对NPOI DLL的引用添加到项目中。在已知位置存储一个您想要的“模板”excel文件,其中包含您需要的任何列标题/格式。然后,您只需使用npoi创建模板文件的副本,并在工作表/行/列级别对其进行操作,然后放入所需的任何数据 示例代码段如下所示。假设您已将输入拆分为字符串列表
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);
}
}
const字符串ExcelTemplateFile=“~/Resources/ExcelInputTemplate.xls”;
常量字符串ExcelWorksheetName=“输出工作表”;
const int required column=1;
专用HSSF工作簿CreateExcelWorkbook(IEnumerable inputData)
{
FileStream fs=newfilestream(Server.MapPath(ExcelTemplateFile)、FileMode.Open、FileAccess.Read);
//正在获取完整的工作簿。。。
HSSF工作簿模板工作簿=新的HSSF工作簿(fs,true);
//正在按工作表的名称获取工作表。。。
HSSFSheet sheet=templateWorkbook.GetSheet(Excel工作表名称);
int startRowIterator=1;
foreach(inputData中的字符串currentData)
{
sheet.CreateRow(startRowIterator).CreateCell(RequiredColumn).SetCellValue(currentData);
}
}
对于生成和操作excel文件,我个人更喜欢库。从Codeplex下载,将对NPOI DLL的引用添加到项目中。在已知位置存储一个您想要的“模板”excel文件,其中包含您需要的任何列标题/格式。然后,您只需使用npoi创建模板文件的副本,并在工作表/行/列级别对其进行操作,然后放入所需的任何数据
示例代码段如下所示。假设您已将输入拆分为字符串列表
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);
}
}
const字符串ExcelTemplateFile=“~/Resources/ExcelInputTemplate.xls”;
常量字符串ExcelWorksheetName=“输出工作表”;
const int required column=1;
专用HSSF工作簿CreateExcelWorkbook(IEnumerable inputData)
{
FileStream fs=newfilestream(Server.MapPath(ExcelTemplateFile)、FileMode.Open、FileAccess.Read);
//正在获取完整的工作簿。。。
HSSF工作簿模板工作簿=新的HSSF工作簿(fs,true);
//正在按工作表的名称获取工作表。。。
HSSFSheet sheet=templateWorkbook.GetSheet(Excel工作表名称);
int startRowIterator=1;
foreach(inputData中的字符串currentData)
{
sheet.CreateRow(startRowIterator).CreateCell(RequiredColumn).SetCellValue(currentData);
}
}
您有管道分隔文本文件的示例吗?@Polity我已经上传了txt文件和excel输出您没有转换您只是用