Click here to Skip to main content
15,918,742 members
Home / Discussions / C#
   

C#

 
GeneralRe: Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet Pin
Walaza8-Apr-08 2:52
Walaza8-Apr-08 2:52 
GeneralRe: Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet Pin
lsconyer8-Apr-08 3:30
lsconyer8-Apr-08 3:30 
GeneralRe: Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet Pin
Walaza8-Apr-08 4:33
Walaza8-Apr-08 4:33 
GeneralRe: Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet Pin
Walaza8-Apr-08 20:08
Walaza8-Apr-08 20:08 
GeneralRe: Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet [modified] Pin
lsconyer9-Apr-08 1:48
lsconyer9-Apr-08 1:48 
GeneralRe: Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet Pin
Walaza9-Apr-08 3:30
Walaza9-Apr-08 3:30 
GeneralRe: Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet Pin
lsconyer9-Apr-08 3:46
lsconyer9-Apr-08 3:46 
GeneralRe: Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet Pin
Walaza9-Apr-08 4:21
Walaza9-Apr-08 4:21 
I tried the code but when I click the button nothing happens. What could I be doing wrong? Here is the code:

//This exports data from the datagrid to a new EXCEL spreadsheet
private void btnExport_Click(object sender, EventArgs e)
{
//Check if the file has been added to the datagrid
if (dgOutput.RowCount == 1)
{
MessageBox.Show("Please load the file first!!", "Error : Load file!!",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
TabControl tbControl = new TabControl();

//loop through each tabpage in tabcontrol.tabpages
foreach (TabPage page in tbControl.TabPages)
{
//get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there
DataGridView dgv = (DataGridView)page.Controls[0];

//loop through each datarow in datagridview
foreach (DataGridViewRow row in dgv.Rows)
{
//create new worksheet
Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass();
_appClass.Visible = true;

_appClass.Caption = "Exported ECS Errors";

Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

Worksheet obj = (Worksheet)wbk.ActiveSheet;

// get the collection of sheets in the workbook
Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets;

Range r = (Range)obj.Cells;

//_appClass.Workbooks.Add(obj);

//load data from datagrid to worksheet
try
{
//Load the Headers and make them BOLD
for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++)
{
r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString();
((Range)r.Cells[1, i + 1]).Font.Bold = true;
}

//Load the data from the datagrid to the new spreadsheet
for (int i = 1; i < ((DataGridView)this.dgOutput).Rows.Count - 1; i++)
{
for (int j = 0; j < ((DataGridView)this.dgOutput).Columns.Count; j++)
{

r[i + 1, j + 1] = ((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString();

//Do the colors
if (((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString() == "Update OK")
{
((Range)r.Cells[i + 1, 1]).Font.Color = Color.Green.G;
}
}
}
}
catch (Exception EX) //user closes the Excel file while it is loading data from the datagrid
{
MessageBox.Show(EX.Message + "The Excel file was closed while loading data from the datagrid.",
"Error : Loading file!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

//add worksheet to workbook
_appClass.Workbooks.Add(sheets);
}

}
}
}

Mvelo Walaza
Developer
Telkom SA

GeneralRe: Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet Pin
lsconyer9-Apr-08 6:09
lsconyer9-Apr-08 6:09 
QuestionHow to write a text in a video files Pin
Smithakrishnan7-Apr-08 2:16
Smithakrishnan7-Apr-08 2:16 
GeneralCustom treeview woes Pin
lsconyer7-Apr-08 2:14
lsconyer7-Apr-08 2:14 
Generalgprs Pin
sbk197-Apr-08 1:52
sbk197-Apr-08 1:52 
GeneralRe: gprs Pin
Dave Kreskowiak7-Apr-08 4:24
mveDave Kreskowiak7-Apr-08 4:24 
GeneralRe: gprs Pin
Luc Pattyn7-Apr-08 4:40
sitebuilderLuc Pattyn7-Apr-08 4:40 
GeneralRe: gprs Pin
Dave Kreskowiak7-Apr-08 6:43
mveDave Kreskowiak7-Apr-08 6:43 
GeneralProblem with merging 2 DataTables Pin
kallileo7-Apr-08 1:26
kallileo7-Apr-08 1:26 
GeneralRe: Problem with merging 2 DataTables Pin
lsconyer7-Apr-08 2:17
lsconyer7-Apr-08 2:17 
GeneralRe: Problem with merging 2 DataTables Pin
kallileo7-Apr-08 2:29
kallileo7-Apr-08 2:29 
GeneralRe: Problem with merging 2 DataTables Pin
lsconyer7-Apr-08 2:39
lsconyer7-Apr-08 2:39 
GeneralRe: Problem with merging 2 DataTables Pin
kallileo7-Apr-08 2:49
kallileo7-Apr-08 2:49 
GeneralRe: Problem with merging 2 DataTables Pin
lsconyer7-Apr-08 2:54
lsconyer7-Apr-08 2:54 
GeneralRe: Problem with merging 2 DataTables Pin
kallileo7-Apr-08 3:11
kallileo7-Apr-08 3:11 
GeneralRe: Problem with merging 2 DataTables Pin
kallileo10-Apr-08 3:20
kallileo10-Apr-08 3:20 
QuestionC#:How to load max no of records in a combobox(more than 40000) Pin
kssknov7-Apr-08 1:18
kssknov7-Apr-08 1:18 
GeneralRe: C#:How to load max no of records in a combobox(more than 40000) Pin
darkelv7-Apr-08 1:37
darkelv7-Apr-08 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.