社区
C#
帖子详情
请问在C#中如何设置Excel文件某一列的对齐方式(比如左对齐),以及某列的宽度。恳请各位大虾帮忙,谢谢!!
cyj2008
2004-12-09 06:53:51
请问在C#中如何设置Excel文件某一列的对齐方式(比如左对齐),以及某列的宽度。
恳请各位大虾帮忙,谢谢!!
...全文
996
7
打赏
收藏
请问在C#中如何设置Excel文件某一列的对齐方式(比如左对齐),以及某列的宽度。恳请各位大虾帮忙,谢谢!!
请问在C#中如何设置Excel文件某一列的对齐方式(比如左对齐),以及某列的宽度。 恳请各位大虾帮忙,谢谢!!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
学习
C#
文件
系统的操作
本课程将介绍如何在
C#
中
执行读写
文件
操作,读写
文件
夹操作。
DataGridView
设置
某列
值的
对齐
方式
DataGridView
设置
某列
值的
对齐
方式
为DataGridview单独某
一列
设置
对齐
样式
this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; //整体
左
对齐
m_regionColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;//指定列右
对齐
样式继承: 顺序靠前的
设置
会覆盖靠后的
设置
:https://docs.microsoft.com/zh-cn/dotnet/framework/w.
C#
实战011:
Excel
操作-获取
Excel
某
一列
数据并存入数组
操作
Excel
中
的数据才是操作
Excel
的意义所在,不然前面忙活那么久就就没任何意义了,同样直接附上代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Office.Inter...
如何在
C#
中
设置
导出
Excel
表格里的单元格宽高
如何在
C#
中
设置
导出
Excel
表格里的单元格宽高 废话少说吧,直奔主题。 先来看一下还没有
设置
样式,直接导出数据时的效果图: 是不是觉得上面的图里的汉字有点靠的太近了?看得不是很顺眼,因为部分的字体被遮住了。 接下来看看
设置
了
宽度
和高度的效果图: 来看一下是如何实现这种效果的。 public ActionResult ExportTo
Excel
(){ //首先查询出你要导出的数据 ...
C#
110,561
社区成员
642,566
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章