代码不在Excel工作表的新单元格中打印数据 [英] Code not printing data in new cells of Excel sheet

查看:61
本文介绍了代码不在Excel工作表的新单元格中打印数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段简单的代码可以打开现有的Excel工作表,并将一些值写入单独的单元格中。



I have a simple piece of code which opens an existing excel sheet, and writes the some values in separate cells.

FileStream ostrm;
                    StreamWriter strm;
                    TextWriter txtWrt = Console.Out;
                    ostrm = new FileStream(path + "\\Output.xls", FileMode.Truncate, FileAccess.ReadWrite);
                    strm = new StreamWriter(ostrm);
                    Console.SetOut(strm);
                    Console.Write("Method" + "\n\t");
                    Console.Write("Sheet" + "\t");
                    Console.Write("Time" + "\t");
                    Console.Write("Type" + "\t");
                    Console.Write("Tab" + "\t");
                    Console.Write("Environment" + "\t");
                    Console.Write("Iteration" + "\t");
                    Console.Write("User" + "\t");
                    Console.Write("Module" + "\t");
                    Console.Write("Sheet" + "\t");
                    Console.WriteLine();
                    Console.SetOut(txtWrt);





但是,此代码打印同一单元格中的所有值,使用'\\ \\ t'作为空间函数而不是切换到新单元格。请指出我出错的地方。



感谢期待!!



However, this code prints all the values in the same cell, using '\t' as a space function rather than switching to a new cell. Please point out where I am going wrong.

Thanks in anticipation!!

推荐答案

正如理查德指出的那样你没有写一个excel文件。您正在制作扩展名为.xls的文本文件。所以当你打开它时,excel打开它并将其显示为文本,可能全部在一个单元格中。



要使用真实的Excel文件,您需要使用Excel.Interop。它当然不像你正在尝试的那样简单。
As Richard pointed out you aren't writing an excel file. You are making a text file that has a .xls extension. So when you open it up, excel opens it and displays it as text probably all in one cell.

To work with real Excel files you will need to use the Excel.Interop. It certainly isn't as easy as what you are attempting.


或者,你正在编写的内容对.csv文件来说更为熟悉。

您可以使用.csv扩展名而不是.xls编写它,MS Excel将打开它。

只需记住Excel使用;作为默认分隔符,或者如果你需要使用\t

,你可以在第一行写sep = \ t。
Alternatively, what you are writing looks more familiar to a .csv file.
You could write it with the .csv extension rather than .xls, and MS Excel will open it.
Just remember that Excel uses ";" as a default separator, or if you necessarily need to use "\t"
you can write "sep=\t" in the first line .


按照 MSDN POST 和上面所有成员说使用Excel.Interop。您也可以此处获得帮助。
Follow this MSDN POST and as all members above said use Excel.Interop. You can get a help here also.


这篇关于代码不在Excel工作表的新单元格中打印数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆