怎么给EXCEL一个区域的单元格加边框 本帖最后由 zhaobinzcb 于 2012-10-19 19:07:26 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以使用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; } 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 这个对象我的关闭命令 是不是错了呀 ra1.Borders.LineStyle = 1; //单元格加边框。 C#字符串和Char的问题 Log4net 询问 工伤保险系统 实现按键功能 winform 中 combobox 怎么实现对listview的类别查询?? 关于.net 消息队列的初级问题 怎样将一行中的一个字进行旋转? 如何取类型的别名?? 实在解决不了,求大神关于TreeView焦点改变后出现的问题 C# 2005 winform datagrid中如何批量修改数据? 用c#做一个计算器(和电脑中的一样) 多表的增删改查
该库使用也比较简单
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;
}
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 这个对象
我的关闭命令 是不是错了呀