如何将一个byte[]写入Excel文件?

发布于 2024-11-26 11:48:31 字数 328 浏览 2 评论 0原文

尽管我掌握了所有基础知识,但我对 C# 非常陌生。我本质上有一个用于加速度计 y 轴(上/下)的字节 [],我将其称为 Y[]。我需要一行代码将其导出到 Excel 文件(我使用 Office professional 2003)。我希望将值Y[0]写入单元格A1,Y[1]写入A2,依此类推(所以只有一列数据。这样做的目的是能够使用excel更轻松地操作数据并能够绘制它。我知道有像 zed graph 这样的 C# 绘图应用程序,但我更喜欢 excel,

我已经广泛查找,但没有找到任何可以帮助我的东西。文件,在上面写字,保存并关闭它。但是,如果您了解我的问题,请给我我需要的一两行代码,

谢谢 !很多人。

I am very new to c# although i control all the basics. I essentially have a byte [] for the y-axis of an accelerometer (up/down) which I called Y[]. I need one line of code to export this to an excel file (I work with 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.

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. But how? Do you use 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖 2024-12-03 11:48:31
  1. 看这里:http://social.msdn.microsoft.com/Forums/en-ZA/csharpgeneral/thread/ef11a193-54f3-407b-9374-9f5770fd9fd7

之后,通过以下方式将字节写入 Excel之前将其转换为字符串。可以在循环中完成 Y[] 数组的遍历。

上面链接中的代码片段

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;

可以解锁您吗?

  1. Look here: 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. Going through your Y[] array can be done in a loop.

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?

烙印 2024-12-03 11:48:31

所以,我最终使用的代码如下。
我有一个字节数组,我不会详细说明它是如何获得的,但它被称为 AllY []。然后我将其转换为一个名为 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();

谢谢大家。我想说这个网站比“官方”微软 msdn 蹩脚网站好 100 倍。

此外,我忘记添加这一点,要使用 Excel 命令,您必须从项目菜单 > > 添加引用。添加参考> COM> Microsoft Excel 11.0 对象(数量可能有所不同)。然后在文档的标题处添加:

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

大家欢呼吧,并保持编码!

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 []. Then I transformed this into a string array called MyYData [] which held the same values but in string form.

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.

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). then at the header of the document, add:

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

Cheers everyone, and keep it coded!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文