Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

C#
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....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900