C#NET2008 Excel Merge Cell Problem

T

teeleong

Hello
I am using C#NET 2008 and Version 2003 of Exce
At Row 1 trying to merge cell from Column 1 to Column 5 into a single cell t
insert the Customer Company name as Banner and my coding is not working du
t
my lack of knowledge

--- Here the the coding. --
private void FExportExcel(

using Microsoft.Office.Interop.Excel

private Microsoft.Office.Interop.Excel.Application objExcel
private Microsoft.Office.Interop.Excel.Workbook objWrkBook
private Microsoft.Office.Interop.Excel.Worksheets objWrkSheet
private Microsoft.Office.Interop.Excel.Range objWrkSheet_range

int intRow = 1
objExcel = new Microsoft.Office.Interop.Excel.Application()
objWrkBook = new objExcel.Workbooks
objWrkSheet = new Worksheet()

//--- merge cells into one --
objWrkSheet =(objWrkSheet.Cells[intRow, 1]
objWrkSheet.Cells[intRow,5]).MergeCells = true

// --- insert Company name into merge cells --
objWrkSheet.Cells[intRow, 1] = "Company Name : "
this.txtCompanyName.Text


Please help me. Once I got it working with you help, As an appreciate of you
help I will post the working coding here to share with other Newbies who ma
have similar problem a

Thank You
Regards
Lennie
 
G

GS

After serious thinking teeleong wrote :
Hello,
I am using C#NET 2008 and Version 2003 of Excel
At Row 1 trying to merge cell from Column 1 to Column 5 into a single cell
to insert the Customer Company name as Banner and my coding is not working
due to
my lack of knowledge.

--- Here the the coding. ---
private void FExportExcel()
{
using Microsoft.Office.Interop.Excel;

private Microsoft.Office.Interop.Excel.Application objExcel;
private Microsoft.Office.Interop.Excel.Workbook objWrkBook;
private Microsoft.Office.Interop.Excel.Worksheets objWrkSheet;
private Microsoft.Office.Interop.Excel.Range objWrkSheet_range;

int intRow = 1;
objExcel = new Microsoft.Office.Interop.Excel.Application();
objWrkBook = new objExcel.Workbooks;
objWrkSheet = new Worksheet();

You're declaring the above variable as a worksheet...
//--- merge cells into one ---
objWrkSheet =(objWrkSheet.Cells[intRow, 1] ,
objWrkSheet.Cells[intRow,5]).MergeCells = true;

Now you're changing the worksheet variable to a Range ref! No can do!!

Try this:
objWrkSheet.Range(cells(1,1), Cells(1,5)).MergeCells = True

or whatever the C# syntax might be to convert the following VB/VBA
syntax...

Range($A$1:$A$5)

where you could load that into a variable declared as a Range object,
merge, then populate Cells(1) of that range with your data. Note that
in merged cells the address of the first cell in the group is the only
one you can use to ref the contents.

HTH
 
D

Dave Peterson

First, I don't speak C#.net.

But in VBA, I'd use something like:

objWrkSheet.Cells(intRow, 1).resize(1,5).mergecells = true

So maybe...

//--- merge cells into one ---
objWrkSheet.Cells[intRow, 1].resize(1,5).mergecells = true;





Hello,
I am using C#NET 2008 and Version 2003 of Excel
At Row 1 trying to merge cell from Column 1 to Column 5 into a single cell to
insert the Customer Company name as Banner and my coding is not working due
to
my lack of knowledge.

--- Here the the coding. ---
private void FExportExcel()
{
using Microsoft.Office.Interop.Excel;

private Microsoft.Office.Interop.Excel.Application objExcel;
private Microsoft.Office.Interop.Excel.Workbook objWrkBook;
private Microsoft.Office.Interop.Excel.Worksheets objWrkSheet;
private Microsoft.Office.Interop.Excel.Range objWrkSheet_range;

int intRow = 1;
objExcel = new Microsoft.Office.Interop.Excel.Application();
objWrkBook = new objExcel.Workbooks;
objWrkSheet = new Worksheet();

//--- merge cells into one ---
objWrkSheet =(objWrkSheet.Cells[intRow, 1] ,
objWrkSheet.Cells[intRow,5]).MergeCells = true;

// --- insert Company name into merge cells ---
objWrkSheet.Cells[intRow, 1] ="Company Name :" +
this.txtCompanyName.Text;
}

Please help me. Once I got it working with you help, As an appreciate of your
help I will post the working coding here to share with other Newbies who may
have similar problem as

Thank You.
Regards.
Lennie
 
D

Dave Peterson

I'd make sure that the ranges were qualified:

objWrkSheet.Range(objWrkSheet.cells(1,1), objWrkSheet.Cells(1,5)).MergeCells _
= True



After serious thinking teeleong wrote :
Hello,
I am using C#NET 2008 and Version 2003 of Excel
At Row 1 trying to merge cell from Column 1 to Column 5 into a single cell to
insert the Customer Company name as Banner and my coding is not working due to
my lack of knowledge.

--- Here the the coding. ---
private void FExportExcel()
{
using Microsoft.Office.Interop.Excel;

private Microsoft.Office.Interop.Excel.Application objExcel;
private Microsoft.Office.Interop.Excel.Workbook objWrkBook;
private Microsoft.Office.Interop.Excel.Worksheets objWrkSheet;
private Microsoft.Office.Interop.Excel.Range objWrkSheet_range;

int intRow = 1;
objExcel = new Microsoft.Office.Interop.Excel.Application();
objWrkBook = new objExcel.Workbooks;
objWrkSheet = new Worksheet();

You're declaring the above variable as a worksheet...
//--- merge cells into one ---
objWrkSheet =(objWrkSheet.Cells[intRow, 1] ,
objWrkSheet.Cells[intRow,5]).MergeCells = true;

Now you're changing the worksheet variable to a Range ref! No can do!!

Try this:
objWrkSheet.Range(cells(1,1), Cells(1,5)).MergeCells = True

or whatever the C# syntax might be to convert the following VB/VBA syntax...

Range($A$1:$A$5)

where you could load that into a variable declared as a Range object, merge,
then populate Cells(1) of that range with your data. Note that in merged cells
the address of the first cell in the group is the only one you can use to ref
the contents.

HTH
 
D

Dave Peterson

ps. I have no idea if you need to use () or [] with that .resize() property.
(Or even if it works in C#.Net.

First, I don't speak C#.net.

But in VBA, I'd use something like:

objWrkSheet.Cells(intRow, 1).resize(1,5).mergecells = true

So maybe...

//--- merge cells into one ---
objWrkSheet.Cells[intRow, 1].resize(1,5).mergecells = true;





Hello,
I am using C#NET 2008 and Version 2003 of Excel
At Row 1 trying to merge cell from Column 1 to Column 5 into a single cell to
insert the Customer Company name as Banner and my coding is not working due
to
my lack of knowledge.

--- Here the the coding. ---
private void FExportExcel()
{
using Microsoft.Office.Interop.Excel;

private Microsoft.Office.Interop.Excel.Application objExcel;
private Microsoft.Office.Interop.Excel.Workbook objWrkBook;
private Microsoft.Office.Interop.Excel.Worksheets objWrkSheet;
private Microsoft.Office.Interop.Excel.Range objWrkSheet_range;

int intRow = 1;
objExcel = new Microsoft.Office.Interop.Excel.Application();
objWrkBook = new objExcel.Workbooks;
objWrkSheet = new Worksheet();

//--- merge cells into one ---
objWrkSheet =(objWrkSheet.Cells[intRow, 1] ,
objWrkSheet.Cells[intRow,5]).MergeCells = true;

// --- insert Company name into merge cells ---
objWrkSheet.Cells[intRow, 1] ="Company Name :" +
this.txtCompanyName.Text;
}

Please help me. Once I got it working with you help, As an appreciate of your
help I will post the working coding here to share with other Newbies who may
have similar problem as

Thank You.
Regards.
Lennie
 
G

GS

I'd make sure that the ranges were qualified:

objWrkSheet.Range(objWrkSheet.cells(1,1), objWrkSheet.Cells(1,5)).MergeCells
_
= True

Absolutely! My bad for not including that in my example.
 
G

GS

Dave Peterson has brought this to us :
First, I don't speak C#.net.

But in VBA, I'd use something like:

objWrkSheet.Cells(intRow, 1).resize(1,5).mergecells = true

So maybe...

//--- merge cells into one ---
objWrkSheet.Cells[intRow, 1].resize(1,5).mergecells = true;

Even better! It's how I'd do it in VB/VBA, but not very informative to
a novice as to what's happening. I was hoping my reply provided some
educating content about how to manipulate the Range object (without
getting too carried away with it)!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top