请问在C#中如何设置Excel文件某一列的对齐方式(比如左对齐),以及某列的宽度。恳请各位大虾帮忙,谢谢!!

cyj2008 2004-12-09 06:53:51
请问在C#中如何设置Excel文件某一列的对齐方式(比如左对齐),以及某列的宽度。

恳请各位大虾帮忙,谢谢!!
...全文
996 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cshape_gis 2004-12-12
  • 打赏
  • 举报
回复
或者你可以录制一个宏,看代码改写一下就行了
cshape_gis 2004-12-12
  • 打赏
  • 举报
回复
Sheet.get_Range(参数1,参数2).HorizontalAlignment=Excel.XlHAlign.xlHAlignCenterAcrossSelection;
XlHAlign下就是对其的方法
tongcheng 2004-12-12
  • 打赏
  • 举报
回复
看看
cyj2008 2004-12-12
  • 打赏
  • 举报
回复
非常感谢各位,尤其是 love040309(04-03-09开始时..) 和cshape_gis(GIS开发者).
love040309 2004-12-12
  • 打赏
  • 举报
回复
private void OutputExcel1(DataSet ds,string str)
{
//
// TODO: 在此处添加构造函数逻辑
//
Excel.Application excel=new Excel.Application( );
int rowIndex=4;
int colIndex=1;

Excel._Workbook xBk;
Excel._Worksheet xSt;

excel.Visible=true;
xBk = excel.Application.Workbooks.Add(true);
xSt = (Excel._Worksheet)xBk.Application.ActiveSheet;
xSt.Name="japy";
//
//取得标题
//
foreach(DataColumn col in ds.Tables["custs"].Columns)
{
colIndex++;
excel.Cells[4,colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
}

//
//取得表格中的数据
//
foreach(DataRow row in ds.Tables["custs"].Rows)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in ds.Tables["custs"].Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 2;
excel.Cells[rowSum,2] = "合计";
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
//
//设置选中的部分的颜色
//
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
//
//取得整个报表的标题
//
excel.Cells[2,2] = str;
//
//设置整个报表的标题格式
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
//
//设置报表表格为最适应宽度
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThick;//设置左边线加粗
xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThick;//设置上边线加粗
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThick;//设置右边线加粗
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThick;//设置下边线加粗
//
//显示效果
//
xBk.Saved=true;
excel.UserControl=false;
excel.ActiveWorkbook.SaveCopyAs(Server.MapPath( ".")+ "\\" +"filename.xls");
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)excel);
GC.Collect();
}
cyj2008 2004-12-12
  • 打赏
  • 举报
回复
先自己顶上去
jackie615 2004-12-09
  • 打赏
  • 举报
回复
学习

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧