MyXLS是一个导出Excel的好工具,速度快,体积小,而且也不用担心使用Com生成Excel时资源释放的问题了。但是作者提供的代码没有设置行高

要实现这个效果,首先需要修改两个文件:

1、Row.cs

添加行高的属性。

private ushort _rowHeight;
 
/// <summary>
/// Gets the row index of this Row object.
/// </summary>
public ushort RowHeight
{
get return _rowHeight; }
set { _rowHeight = value; }
}

在构造函数中设置默认值:

public Row()
        {
            _minCellCol = 0;
            _maxCellCol = 0;
            _rowHeight = 280;
        }

2、RowBlocks.cs

在生成字节时,将这个高度设置进去。

在private static Bytes ROW(Row row)方法中,大约150行左右。

注释掉原来的:

bytes.Append(new byte[] { 0x08, 0x00 });

修改为:

if (row.RowHeight > 32767)
{
throw new ApplicationException("Row height can not greater than 32767.");
}
else
{
bytes.Append(BitConverter.GetBytes(row.RowHeight));
}

注释掉:

bytes.Append(new byte[] {0x00, 0x01, 0x0F, 0x00});

替换为:

//Bit Value
            //7   1
            //6   1      Row height and default font height do not match
            //5   0
            //4   0
            //2-0 0
            bytes.Append(new byte[] { 0xC0, 0x01, 0x0F, 0x00 });

这样保存修改,重新生成类库,重新添加到网站引用。

下边看看怎么用:

/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ExportBtn_Click(object sender, EventArgs e)
{
XlsDocument xls = new XlsDocument();
xls.FileName = "TestList.xls";
 
int rowIndex = 1;
Worksheet sheet = xls.Workbook.Worksheets.Add("测试表");//Sheet名称
 
Cells cells = sheet.Cells;
Cell cell = cells.Add(1, 1, "编号");
cell.Font.Bold = true;
cell = cells.Add(1, 2, "名称");
cell.Font.Bold = true;
 
sheet.Rows[1].RowHeight = 18 * 20;
 
foreach (DataRow row in table.Rows)
{
cells.Add(rowIndex, 1, rowIndex);
cells.Add(rowIndex, 2, "名称"+rowIndex);
 
rowIndex++;
}
xls.Send();
}

在添加标题行cell之后,添加了一行:

sheet.Rows[1].RowHeight = 18 * 20;

这一行必须写在添加完cell之后,因为添加cell的时候才会自动创建一个Row对象,之前是没有这个对象的,当然不能设置高度。

这样就可以设置行高了,是不是很简单。

可是我还想让行高的设置方式更优雅一些,就像设置列的宽度一样,比如设置列:

ColumnInfo col1 = new ColumnInfo(xls, sheet);//生成列格式对象
col1.ColumnIndexStart = 0;//起始列为第1列
col1.ColumnIndexEnd = 0;//终止列为第1列
col1.Width = 8 * 256;//列的宽度计量单位为 1/256 字符宽
sheet.AddColumnInfo(col1);//把格式附加到sheet页上

这样就可以设置第一列的宽度,这段程序放在添加cell前后都没有任何问题,而且可以设置一个列的范围,不用一个个设置。

就仿造这个模式,创建一个RowInfo:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace org.in2bits.MyXls
{
/// <summary>
/// Describes a range of rows and properties to set on those rows (column height, etc.).
/// </summary>
public class RowInfo
{
private ushort _rowHeight;
private ushort _rowIdxStart = 0;
private ushort _rowIdxEnd = 0;
 
/// <summary>
/// Gets or sets height of the rows.
/// </summary>
public ushort RowHeight
{
get return _rowHeight; }
set { _rowHeight = value; }
}
 
/// <summary>
/// Gets or sets index to first row in the range.
/// </summary>
public ushort RowIndexStart
{
get return _rowIdxStart; }
set
{
_rowIdxStart = value;
if (_rowIdxEnd < _rowIdxStart)
_rowIdxEnd = _rowIdxStart;
}
}
 
/// <summary>
/// Gets or set index to last row in the range.
/// </summary>
public ushort RowIndexEnd
{
get return _rowIdxEnd; }
set
{
_rowIdxEnd = value;
if (_rowIdxStart > _rowIdxEnd)
_rowIdxStart = _rowIdxEnd;
}
}
}
}

这个类有三个属性:行高、起始行索引、结束行索引。

还需要一个方法附加到sheet页上。

在Worksheet类中添加一个私有变量:

private readonly List<RowInfo> _rowInfos = new List<RowInfo>();

然后添加一个方法用于添加RowInfo到集合中:

/// <summary>
/// Adds a Row Info record to this Worksheet.
/// </summary>
/// <param name="rowInfo">The RowInfo object to add to this Worksheet.</param>
public void AddRowInfo(RowInfo rowInfo)
{
_rowInfos.Add(rowInfo);
}

下一步要在生成字节码之前设置相关行的高度,修改属性:internal Bytes Bytes,大约在252行左右:

get
{
//Set row height
int rowsCount = Rows.Count;
for (ushort i = 1; i <= Rows.Count; i++)
{
foreach (RowInfo rowInfo in _rowInfos)
{
if (rowInfo.RowIndexStart <= i && rowInfo.RowIndexEnd >= i)
{
Rows[i].RowHeight = rowInfo.RowHeight;
break;
}
}
}
 
....
 
}

在get中最前面遍历行设置行高,这里也许可以优化下效率,有兴趣的看看吧。

现在保存所有文件,重新编译,重新引用。

现在再来看看怎么设置行高:

RowInfo rol1 = new RowInfo();
rol1.RowHeight = 16 * 20;
rol1.RowIndexStart = 3;
rol1.RowIndexEnd =10;
sheet.AddRowInfo(rol1);

从第3行到第10行,行高都是16。行高是以1/20 point为单位的。

现在有两种方法设置行高了,强大吧。

我把编译好的DLL提供下载,原版本是.net2.0的框架,我改成了.net4.0框架

点击这里:下载

实现Myxls设置行高的功能(转)的更多相关文章

  1. DataGridView设置行高

    .Net中DataGridView控件如何设置行高 在DataGridView控件中,默认的行高很大,而标题头的行高却很小,感觉很不匀称. 标题头的行高比较好设置需要修改两个属性1修改ColumnHe ...

  2. 闲记 单元格与单元格之间的边 ///字体属性都是font开头,除了颜色属性 ///文本属性都是text开的,除了设置行高。

    这两天一直在做网页没有什么太大的问题,期间也考了一场试,对答案的时候老师讲了一些小知识,因此来记录一下. 单元格与单元格之间的边距(cellspaling) list-type-image可以使用图像 ...

  3. angularjs ui-grid如何动态设置行高

    自己开发的公众号,可以领取淘宝内部优惠券 在用ui-grid的时候我们可以用rowHeight设置行高,可是每一行的高度都是一样的,无法根据行内的内容进行自适应.如下图 为了解决这个问题,google ...

  4. 给GridView设置行高

    近期在工作中遇到了这样一个问题,使用一个GridView展示数据,item中仅仅是一个TextView,可是里面显示的文字多少不固定多少,必须所有展示出来. 遇到的问题: 1.把item中的宽和高设置 ...

  5. 如何给HTML页面设置行高

    设置行高 由于简单还是老样子直接上代码了哦,注意:line-height属性值可以使用固定值如:20px..和百分比如:20%. 如果想让文字垂直居中如下:行高的主要作用是用来设置文本的垂直方向居中对 ...

  6. Nopi 导出设置行高

    1.导出excel行显示不完整数据客户不满意,需要我们处理 ; rowNum <= sheet.LastRowNum; rowNum++) { HSSFRow currentRow = shee ...

  7. 实现jsp网页设为首页功能

    var url = location.href; var browser_name = navigator.userAgent; if(browser_name.indexOf('Chrome')!= ...

  8. 在safari下input的placeholder设置行高失效

    在项目中遇到input的placeholder在safari下设置行高失效的问题,亲测 input{ width:250px; height:30px; line-height:30px; font- ...

  9. 20190316xlVba_设置行高的改进方案

    Public Sub AutoSetRowHeight(ByVal sht As Worksheet, Optional RowsInOnePage As Long) Dim BreakRow As ...

随机推荐

  1. 关于angularjs指令

    一个指令用来引入新的HTML语法.指令是DOM元素上的标记,使元素拥有特定的行为.举例来说,静态的HTML不知道如何来创建和展现一个日期选择器控件.让HTML能识别这个语法,我们需要使用指令.指令通过 ...

  2. git 命令熟悉

    1. git clone +ssh 地址=将远程代码download 到本地:要在根目录执行这个操作 2.查看所有分支:git branch -a (当前分支前带星号) 3.切换到某个分支:git c ...

  3. 汗,Google又调整了编译工具(升级SDK先备份!!!)

    1./tools 下的apkbuilder消失了 方法一.用老版本ADT中的apkbuilder(apkbuilder.bat--windows) 方法二.重新生成build.xml文件 2.aapt ...

  4. android adb 命令详解

    ADB (Android Debug Bridge)  是android SDK中的工具,需要先配置环境变量才能使用.起调试桥的作用,可以管理安卓设备.(也叫debug工具) ---------查看设 ...

  5. 搭建Nginx+Java环境测试并且运行

    一.简介: Tomcat在高并发环境下处理动态请求时性能很低,而在处理静态页面更加脆弱.虽然Tomcat的最新版本支持epoll,但是通过Nginx来处理静态页面要比通过Tomcat处理在性能方面好很 ...

  6. IDEA构建一个mybatis项目

    目录结构如下: 在pom.xml中配置需要的jar包 <dependencies> <dependency> <groupId>org.mybatis</gr ...

  7. 【笔记】js操作cookie

    $.cookie('the_cookie'); // 读取 cookie            $.cookie('the_cookie', 'the_value', { expires: 7 }); ...

  8. 第四课 开发uehtml官网响应式静态页面

    概况:整站布局.头部菜单响应式设置.最新消息模块变化.内容模块四三二响应式变化. 伪类选择器: E:nth-of-type(n)  表示E父元素中的第n个字节点,且类型为E      E:nth-la ...

  9. facebook 简单的图文分享

    参考网址:http://www.mrfangge.com/facebook-page-share-settings/ 首先在facebook 注册开发者,新增一个应用,这里就要一个生所的应用 app_ ...

  10. Docker和Docker-compose安装教程以及docker-elk,docker-storm安装教程

    此安装教程仅供我自己安装配置时查看,其他的人不可以偷看!!! 安装Docker 1. Update package information, ensure that APT works with th ...