Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello
I am struggling how to:

if a user click's on information shown in the list box that i have populated my database with, and they click on a "export to excel" button the program should export relevant information about the selected list box item in a spreadsheet.

code will be appreciated but otherwise any info will be helpful

[Edit]
OP's code from the comments:

private void button2_Click(object sender, EventArgs e) 
{
Excel.Application xlApp ; 
Excel.Workbook xlWorkBook ; 
Excel.Worksheet xlWorkSheet ; 
object misValue = System.Reflection.Missing.Value; 
xlApp = new Excel.ApplicationClass(); 
xlWorkBook = xlApp.Workbooks.Add(misValue); 
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
 
int i = 0; int j = 0; 
for (i = 0; i <= dataGridView1.RowCount - 1; i++) 
{ 
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++) 
{ 
DataGridViewCell cell = dataGridView1[j, i]; 
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value; 
} 
} 
xlWorkBook.SaveAs("csharp.net-informations.html", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
 
xlWorkBook.Close(true, misValue, misValue); 
xlApp.Quit(); releaseObject(xlWorkSheet); 
releaseObject(xlWorkBook); releaseObject(xlApp); 
MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");

[/Edit]

can anyone help me please!

what i meant is
Posted 9-Aug-12 11:59am
Edited 9-Aug-12 12:51pm
v4
Comments
Wes Aday at 9-Aug-12 17:02pm
   
"how do i allow a user"... any way you want. Not sure what code you are expecting. This is very vague.
inzefinite at 9-Aug-12 17:32pm
   
what i meant is if a user click's on information shown in the list box that i have populated my database with, and they click on a "export to excel" button the program should export relevant information about the selected list box item in a spreadsheet

sorry if im not making sense at all, i am trying to explain correctly.
Wes Aday at 9-Aug-12 17:56pm
   
It's not that you are not making any sense at all. It's hard to get what it is that you are asking. Obviously, from your code snippet, you know how to create a button click, get data from controls and write to Excel. So what is it exactly that you are having trouble figuring out?
inzefinite at 9-Aug-12 17:59pm
   
thnx, i got it to work still cant see what went wrong but thanx
losmac at 9-Aug-12 17:11pm
   
What have you done till now?
Show your code!
inzefinite at 9-Aug-12 17:32pm
   
i don't have code for it yet, i try code and it doesn't work so i delete and try again. so basically im at the starting point

but it should be something like this:


Quote:
private void button2_Click(object sender, EventArgs e)
{
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;

for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}

xlWorkBook.SaveAs("csharp.net-informations.html", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Some small commetns to your code:

1. Always use excel in a try catch block, they are prone to errors outside your control.

2. You should use xlWorkSheet.Cells[i + 1, j + 1].Value = cell.Value.ToString(). The excel cell has many fields .Formula .Format etc, you should always specify whitch one you use, and it can in some circumstances generate an error.

The last thing I would say is that you can also use
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Active; There are some excel versions that starts with three blank sheets (I think this is the default setting) active would normally get the first.

[Edit]Also always include the dll file form the excel version you are using, small differences in versions can make it crash otherwise.[/Edit]

Otherwise; Happy coding Wink | ;)
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,474
1 OriginalGriff 6,258
2 Peter Leow 2,534
3 Abhinav S 2,274
4 Maciej Los 2,268


Advertise | Privacy | Mobile
Web02 | 2.8.150414.1 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100