开发者

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

开发者 https://www.devze.com 2023-03-23 10:43 出处:网络
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

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.


  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?


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号