怎么给EXCEL一个区域的单元格加边框

zhaobinzcb 2012-10-19 07:05:17
Range ra1;
ra1=sheet.get_Range(excel.Cells[1, 1], excel.Cells[5, 3]);
ra1.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
ra1 = null;


我这个的结果就是这个区域所有的单元格外面有一个大的边框
我希望那种 所有边框的效果 就是每个单元格都有边框

应该怎么写
还有 关闭这个对象的命令我用
ra1 = null;
对吗
...全文
547 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
c210501053 2013-05-03
  • 打赏
  • 举报
回复
ra1.Borders.LineStyle = 1; //单元格加边框。
zhaobinzcb 2012-10-20
  • 打赏
  • 举报
回复

Range ra1;
ra1=sheet.get_Range(excel.Cells[1, 1], excel.Cells[5, 3]);
ra1.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
ra1 = null;

我在循环里用的这个代码
总是报错
我感觉是 没有注销 ra1 这个对象
我的关闭命令 是不是错了呀
xc人生 2012-10-20
  • 打赏
  • 举报
回复
可以子啊网上游许多的例子
zhaobinzcb 2012-10-20
  • 打赏
  • 举报
回复
我就是做一个小东西 把这个边框搞定就可以了
从新学习一个库 就算了吧 还是
skyering 2012-10-20
  • 打赏
  • 举报
回复
可以使用NPOI库,下面是我用NOPI库导出数据的格式。


该库使用也比较简单
private void InitWorksheet(Sheet sheetUser)
{
//创建结构
CreateHeaderForSheet(sheetUser);

//设置列宽
sheetUser.SetColumnWidth(0, 10 * 256);
sheetUser.SetColumnWidth(1, 50 * 256);
sheetUser.SetColumnWidth(2, 10 * 256);
sheetUser.SetColumnWidth(3, 20 * 256);
//冻结行
sheetUser.CreateFreezePane(0, 1);
}

/// <summary>
/// 创建结构
/// </summary>
/// <param name="sheet"></param>
private void CreateHeaderForSheet(Sheet sheet)
{
const int rowHeaderIndex = 0;
Row row = sheet.CreateRow(rowHeaderIndex);

Cell cell1 = row.CreateCell(0);
cell1.SetCellValue("机构编号");
Cell cell2 = row.CreateCell(1);
cell2.SetCellValue("机构名称");
Cell cell3 = row.CreateCell(2);
cell3.SetCellValue("用户名");
Cell cell4 = row.CreateCell(3);
cell4.SetCellValue("密码");

//- 加粗,白色前景色
var cellFont = sheet.Workbook.CreateFont();
cellFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
cellFont.FontHeight = 200;
cellFont.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;

var cellStyle = sheet.Workbook.CreateCellStyle();
cellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
cellStyle.FillForegroundColor = HSSFColor.LIGHT_ORANGE.index;
cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER;
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
cellStyle.BorderBottom = CellBorderType.THIN;
cellStyle.BorderLeft = CellBorderType.THIN;
cellStyle.BorderRight = CellBorderType.THIN;
cellStyle.BorderTop = CellBorderType.THIN;

cellStyle.SetFont(cellFont);


cell1.CellStyle = cellStyle;
cell2.CellStyle = cellStyle;
cell3.CellStyle = cellStyle;
cell4.CellStyle = cellStyle;
}
zhaobinzcb 2012-10-20
  • 打赏
  • 举报
回复
谁帮我看一下呀 谢谢了

110,532

社区成员

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

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

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