Hi
I having a list box which bind some text in the list box. When i select the item in the list box and click on the export button, excel need to get exported with selected text as column name in the exported excel.
Can any one give me an idea about how to achieve this in c#.
Thanks.
Export the selected text from the list box to excel in C#
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.


Sunny SharmaRank #223 · 11.1K reputationPosted May 27, 2013, 10:53 PM EDT
Below is a solution for you to Export selected Items in Excel:
You need to add references for
Microsoft.Office.Interop.Excel and
System.Reflection
Missing mv = Missing.Value;
private void btnExport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application NewExcelApp = new Microsoft.Office.Interop.Excel.Application();
_Workbook wkb = NewExcelApp.Workbooks.Add(mv);
_Worksheet wks = wkb.Sheets[1];
for (int i = 1; i < ListView1.SelectedItems.Count; i++)
{
((Range)wks.Cells[1, i]).Value = ListView1.SelectedItems[i-1].Text;
}
object filePath = "D:\\testExcelExport";
wkb.SaveAs(filePath, XlFileFormat.xlExcel8, mv, mv, mv, mv, XlSaveAsAccessMode.xlNoChange, mv, mv, mv, mv, mv);
wks = null;
wkb = null;
NewExcelApp.Quit();
MessageBox.Show("Done");
}
This will achieve your purpose.
Please don't forget o accept this as answer if it helps!
Thanks