469,366 Members | 2,273 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Simple C# to Excel Export

Hi everyone
I've got a very basic program that lists drivespace stats of important network computers. I want to export the variable data to excel so i can do a trend analysis over a given time to help predict when our servers will run out of space.
No fancy formatting is needed.

Any help will be much appreciated

Thanks
Gustav
May 22 '07 #1
5 36131
to answer my own question...:)
i figured it out, much simpler than i thought
so if anyone has a similar question, hope this helps:
it pulls data out of a listview and pumps it into Excel 2007.
Make sure you have the correct references.
Expand|Select|Wrap|Line Numbers
  1. Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
  2.  
  3.             xla.Visible = true;
  4.             Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
  5.             Worksheet ws = (Worksheet)xla.ActiveSheet;
  6.             int i = 1;
  7.             int j = 1;
  8.             foreach (ListViewItem comp in lvwResults.Items)
  9.             {
  10.                 ws.Cells[i, j] = comp.Text.ToString();
  11.                 //MessageBox.Show(comp.Text.ToString());
  12.                 foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)
  13.                 {
  14.                     ws.Cells[i, j] = drv.Text.ToString();
  15.                     j++;
  16.                 }
  17.                 j = 1;
  18.                 i++;
May 22 '07 #2
kenobewan
4,871 Expert 4TB
Well done! Thanks for sharing the solution.
May 22 '07 #3
I have three list views in my code. How I can export each one to a specific sheet in the excel file? How I can also give each sheet name.
Apr 14 '09 #4
How do i test this code? Do i just copy this code and paste it in Visual studio? Im new to this. Need guidance. Thanks.
Dec 23 '09 #5
Her77
1
Hi, I'm Hernn from Argentina. I found the post very useful! Thanks a lot! Congratulations!
Jul 27 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Eric Johannsen | last post: by
reply views Thread by Max Mayer | last post: by
reply views Thread by Max Mayer | last post: by
reply views Thread by arnab | last post: by
6 posts views Thread by Sergio E. | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.