Click here to Skip to main content
11,717,620 members (81,751 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
private void LoadDetails()
{
try
{
SqlDataReader Xldr;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
Excel.Range chartRange;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Int32 xlRow;

//load the course details
sql = "select course FROM Tb_SCh_TIme_Table group by course order by course ";
dr = GFun.ReadSql(sql);
xlRow = 3;
while (dr.Read())
{
//ColName.Name = dr[0].ToString().Trim();
xlWorkSheet.Cells[xlRow,1] = dr[0].ToString().Trim();
chartRange = xlWorkSheet.get_Range("A" + xlRow, "E" + xlRow);
//chartRange.Cells.HorizontalAlignment = xl
chartRange.MergeCells = true;
//chartRange.Cells.Font = FontStyle.Bold;
chartRange.Cells.Font.Bold = true ;


xlRow = xlRow + 1;
xlWorkSheet.Cells[xlRow, 1] = "Date";
xlWorkSheet.Cells[xlRow, 2] = "Session 1";
xlWorkSheet.Cells[xlRow, 3] = "Session 2";
xlWorkSheet.Cells[xlRow, 4] = "Session 3";
xlWorkSheet.Cells[xlRow, 5] = "Session 4";
xlRow = xlRow + 1;

sql = "select * FROM Tb_SCh_TIme_Table P PIVOT ";
sql = sql + "(MAX(Faculty_Code) FOR Session IN ([1],[2],[3] ,[4])) AS PVT ";
sql = sql + " where course = '" + dr[0].ToString().Trim() + "' order by date";
Xldr = GFun.ReadSql(sql);
while (Xldr.Read())
{
xlWorkSheet.Cells[xlRow, 1] = Xldr[1].ToString();
xlWorkSheet.Cells[xlRow, 2] = Xldr[3].ToString();
xlWorkSheet.Cells[xlRow, 3] = Xldr[4].ToString();
xlWorkSheet.Cells[xlRow, 4] = Xldr[5].ToString();
xlWorkSheet.Cells[xlRow, 5] = Xldr[6].ToString();
if (Xldr[4].ToString().Trim() == "")
{

}

Xldr.Close();
xlRow = xlRow + 2;
}
dr.Close();

xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
MessageBox.Show("File created !");
}
catch (Exception Ex1)
{
MessageBox.Show(Ex1.ToString(), "Error");
}
}


from my code i merge the cell but i want the merge cells to be in CENTRE. from my code please tell how can i do.
Posted 21-Jan-13 21:08pm

1 solution

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

Solution 1

chartRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
           chartRange.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;


add this after merge statement.
hope it will work....
  Permalink  

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 809
1 OriginalGriff 215
2 F-ES Sitecore 210
3 Maciej Los 140
4 Jochen Arndt 120
0 Sergey Alexandrovich Kryukov 1,494
1 OriginalGriff 535
2 Maciej Los 529
3 Richard MacCutchan 440
4 CHill60 415


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 22 Jan 2013
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