简体   繁体   English

如何将字节[]写入Excel文件?

[英]How do I write a byte [] into an Excel file?

I am very new to c# although i control all the basics. 尽管我掌握所有基础知识,但我对C#还是很陌生。 I essentially have a byte [] for the y-axis of an accelerometer (up/down) which I called Y[]. 实际上,对于加速度计的y轴(上/下),我有一个字节[],我称之为Y []。 I need one line of code to export this to an excel file (I work with Office professional 2003). 我需要一行代码将此导出到excel文件(我使用Office Professional 2003)。 I would like value Y[0] to be written to cell A1, Y[1] to A2, and so on ( so just one column of data. The purpose of this is to be able to use excel to manipulate the data more easily and be able to plot it. I know there are plotting apps for c# like zed graph but i preffer excel. 我希望将值Y [0]写入单元格A1,将Y [1]写入单元格A2,依此类推(因此只写入一列数据。这样做的目的是能够使用excel更轻松地操作数据并能够绘制它。我知道有像zed graph这样的c#绘制应用程序,但我比较擅长。

I have looked far and wide and havent found anything to help me. 我四处张望,还没有找到任何可以帮助我的东西。 All i need to do is create an excel (.xls) file, write on it, save it and close it. 我需要做的就是创建一个excel(.xls)文件,对其进行写入,保存并关闭。 But how? 但是如何? Do you use FileStream? 您是否使用FileStream? or what? 要不然是啥? please if you understand my problem give me the one or two lines of code i need. 如果您理解我的问题,请给我我需要的一两行代码。 I'm so close to finishing this little project!!! 我已经接近完成这个小项目了!!!

Thanks a lot guys. 非常感谢。

  1. Look here: http://social.msdn.microsoft.com/Forums/en-ZA/csharpgeneral/thread/ef11a193-54f3-407b-9374-9f5770fd9fd7 在这里查看: http : //social.msdn.microsoft.com/Forums/en-ZA/csharpgeneral/thread/ef11a193-54f3-407b-9374-9f5770fd9fd7

After that, write your byte into Excel by converting it to a String before. 之后,先将字节转换为字符串,然后将其写入Excel。 Going through your Y[] array can be done in a loop. 遍历Y []数组可以循环完成。

Code Snippet from link above 上方链接的代码段

Excel.Application excelApp = new Excel.Application();
string myPath = @"C:\Excel.xls";
excelApp.Workbooks.Open(myPath);
int rowIndex = 1; int colIndex = 1;

excelApp.Cells[rowIndex, colIndex] = "First";
excelApp.Visible = true;

Does that unblock you? 这样可以解除您的封锁吗?

So, the code I ended up using was the following. 因此,我最终使用的代码如下。 I had a byte array which i'm not gonna go into details as to how it got it, but it was called AllY []. 我有一个字节数组,我不愿详细介绍它的获取方式,但它被称为AllY []。 Then I transformed this into a string array called MyYData [] which held the same values but in string form. 然后,我将其转换为一个名为MyYData []的字符串数组,该数组保留相同的值,但为字符串形式。

for (int F = 0; F < (21*SECT); F++)
{
     Console.WriteLine(AllY[F]);   // Shows the byte array mentioned.
     MyYData[F] = AllY[F].ToString();  // The data is sotred as succesions of strings.
}
Console.ReadKey();

Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
string myPath = @"C:\Documents and Settings\John\My Documents\DATA.xls";   // The main downside to this code, is that the document must exist prior to code execution (but i'm sure you guys can figure out a way for the code to create de document).
excelApp.Workbooks.Open(myPath);                                                            

for (int r = 1; r < ((21 * SECT)+1); r++)  // r must be set to 1, because cell 0x0 doesn't exist!
{
     int rowIndex = r;
     int colIndex = 1;
     excelApp.Cells[rowIndex, colIndex] = MyYData[r-1];
     excelApp.Visible = true;
}
Console.ReadKey();

Thank you everyone. 谢谢大家。 And i'd like to say that this website works 100 times better than the "official" microsoft msdn crappy site. 我想说的是,该网站比“正式”的Microsoft msdn糟糕的网站要好100倍。

Furthermore, i forgot to add that to use the excel commands you have to add a reference from the project menu > Add Reference > COM > Microsoft Excel 11.0 object (the number may vary). 此外,我忘记添加它以使用excel命令,您必须从项目菜单>添加引用> COM> Microsoft Excel 11.0对象添加引用(数量可能有所不同)。 then at the header of the document, add: 然后在文档的标题处添加:

using Microsoft.Office.Core; 使用Microsoft.Office.Core; using Excel = Microsoft.Office.Interop.Excel; 使用Excel = Microsoft.Office.Interop.Excel;

Cheers everyone, and keep it coded! 欢呼大家,并保持编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM