MyXLS是一个导出Excel的好工具,速度快,体积小,而且也不用担心使用Com生成Excel时资源释放的问题了。MyXLS已经实现了很多操作Excel的功能,但是作者好像2009年末就不再更新维护这个程序了,猜想可能是新版本office的文件格式操作起来更容易。我这里管不了这么多啊,还得继续用。
作者在程序中注释说将要实现自定义行高的功能,但是一等就是两年,还不知道猴年马月。还好程序是开源的,作者也给了提示,参考excelfileformat.pdf这个文档第196页,我把这个功能给实现了。
先看看效果如何:
要实现这个效果,首先需要修改两个文件:
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为单位的。
现在有两种方法设置行高了,强大吧。
修改起来挺烦人,想直接使用的朋友点击这里下载就可以了。
欢迎测试!

大哥 我想问下,假如我想生成只读的xls呢? 怎么实现啊 或者有什么解决方案啊。谢谢了
@kouweicai
只读的Excel可以通过设置修改密码来实现,如果想通过修改程序来实现可以看看excelfileformat.pdf文档中的4.2、4.18、5.82节。
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)
{
Rows[i].RowHeight = rowInfo.RowHeight;
break;
}
}
}
….
}
这个点点点是什么意思啊?get有返回值的,是要替换掉整个get方法吗?
@SOLO
… 是省略掉原来的,是在get中添加这部分,不是全部替换。
大哥,能把原码放出来不?
@xuzx
代码直接去官方网站下载就行了,用SVN或者到这个地址:http://sourceforge.net/projects/myxls/files/MyXls/v0.6.3/,然后加上这些修改。文件有点大所以就不放上来了,我发你邮箱吧。
@jeffcn2
发上来的代码漏掉了一部分,已经补上,请确认!
大哥,我昨天按你说博客里所说的去修改代码,但是还是设置不了行高,大哥您是否能给我个邮箱,我把我修改的代码发你,你帮我点拨一下呢?
bossma@yeah.net
神人啊
发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论