问题描述
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;
//加载课程详细信息
sql =选择课程FROM Tb_SCh_TIme_Table group by course course 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] =会话2;
xlWorkSheet.Cells [xlRow,4] =会话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()+''按日期排序;
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。退出();
releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
MessageBox.Show(File created!);
}
catch(Excep EX1)
{
MessageBox.Show(Ex1.ToString(),错误);
}
}
来自我的代码我合并了单元格,但我希望合并单元格在CENTER中。从我的代码请告诉我该怎么办。
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.
推荐答案
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....
这篇关于如何在中心合并单元格我发送代码帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!