LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.framework.windowsforms.controls,microsoft.public.excel.programming
SVD SVD is offline
external usenet poster
 
Posts: 1
Default ListView to Excel Code (but needs SPEED improvements)

Hi,

The following code works, but it has some issues:

1) Any suggestions to optimize the code?
2) How can I set the text and background colors for my header cells

Thanks,

Steve


private void button1_Click(object sender, System.EventArgs e)
{
// Create application
Excel.Application excel = new Excel.Application();

if (excel == null)
{
MessageBox.Show("ERROR: EXCEL could not be started!");
return;
}

// Add 1 workbook with 1 sheet
Excel.Workbook workbook =
excel.Workbooks.Add(Excel.XlWBATemplate.xlWBATWork sheet);
Excel.Worksheet workSheet =
(Excel.Worksheet)workbook.ActiveSheet;

// Add column headers
for (int columnIndex=0; columnIndex<listView1.Columns.Count;
columnIndex++)
{
workSheet.Cells[1, columnIndex+1] =
listView1.Columns[columnIndex].Text;
((Excel.Range)(workSheet.Cells[1, columnIndex+1])).Font.Bold
= true;
}

// Add data rows
for (int rowIndex=0; rowIndex<listView1.Items.Count; rowIndex++)
{
for (int columnIndex=0; columnIndex<listView1.Columns.Count;
columnIndex++)
{
workSheet.Cells[rowIndex+2, columnIndex+1] =
listView1.Items[rowIndex].SubItems[columnIndex].Text;
}
}

// Autofit
workSheet.Columns.AutoFit();

// Show
excel.Visible = true;

// Clean up
excel = null;
workBook = null;
workSheet = null;
GC.Collect();
}


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to Speed Up A Code LostInNY Excel Worksheet Functions 2 July 20th 09 06:18 PM
Overview Improvements for Excel Worksheets Feda Excel Discussion (Misc queries) 5 September 18th 07 10:08 PM
Improvements in Excel Pivot Table Access4Afghan Excel Discussion (Misc queries) 5 February 12th 06 02:41 PM
Listview in Excel 97 vba. jaxrpc Excel Programming 3 January 1st 04 03:04 PM
Analyzing code speed mbobro[_2_] Excel Programming 1 November 3rd 03 10:05 PM


All times are GMT +1. The time now is 06:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"