MyXls组件操作Excel报表(C#)
在后台调用excel组件,生成Excel,虽然可以对Excel文件进行完全控制,可以生成任何复杂的格式,但是有个很大的缺点,这种方式会产生很多Excel进程,很难完全清除掉,特别是在出错的时候,可能会使整个服务器崩溃。本文为大家介绍一个C#写的开源组件,并简单说下office2003和以上版本支持的XML格式。
一 操作Excel二进制格式
OpenOffice.org发布过的俩个文档Excel File Format (BIFF8)Specification和Microsoft CompoundDocument (OLE2) Format Specification对Excel的二进制格式做了一个比较详细的说明,依靠这些信息,我们可以直接操作Office二进制格式文档。
MyXls是一个C#写的开源组件,可以用来生成具有很多表格且包含格式的Excel文件。它提供了一套基于对象的API,非常容易使用。
MyXls这个组件的DLL 和代码 可以在这下载到:
http://sourceforge.net/project/showfiles.php?group_id=205384&package_id=245371
以下是一个事例子。。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using org.in2bits.MyXls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
XlsDocument xls = new XlsDocument();
Worksheet sheet = xls.Workbook.Worksheets.Add("sheet");//状态栏标题名称
Cells cells = sheet.Cells;
cells.Add(1, 1, "Name");
cells.Add(1, 2, "Age");
cells.Add(1, 3, "Email");
cells.Add(1, 4, "Description");
for (int i = 0; i < 5000; i++)
{
cells.Add(i + 2, 1, "netflu");
cells.Add(i + 2, 2, 28.ToString());
cells.Add(i + 2, 3, "xxx@163.com");
cells.Add(i + 2, 4, "sheet.Cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//将数据添加到xls表格里");
}
//foreach (DataRow row in table.Rows)
//{
// rowIndex++;
// colIndex = 0;
// foreach (DataColumn col in table.Columns)
// {
// colIndex++;
// //sheet.Cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//将数据添加到xls表格里
// Cell cell = cells.AddValueCell(rowIndex, colIndex, Convert.ToDouble(row[col.ColumnName].ToString()));//转换为数字型
// //如果你数据库里的数据都是数字的话 最好转换一下,不然导入到Excel里是以字符串形式显示。
// cell.Font.FontFamily = FontFamilies.Roman; //字体
// cell.Font.Bold = true; //字体为粗体
// }
//}
xls.Send();
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using org.in2bits.MyXls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
XlsDocument xls = new XlsDocument();
Worksheet sheet = xls.Workbook.Worksheets.Add("sheet");//状态栏标题名称
Cells cells = sheet.Cells;
cells.Add(1, 1, "Name");
cells.Add(1, 2, "Age");
cells.Add(1, 3, "Email");
cells.Add(1, 4, "Description");
for (int i = 0; i < 5000; i++)
{
cells.Add(i + 2, 1, "netflu");
cells.Add(i + 2, 2, 28.ToString());
cells.Add(i + 2, 3, "xxx@163.com");
cells.Add(i + 2, 4, "sheet.Cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//将数据添加到xls表格里");
}
//foreach (DataRow row in table.Rows)
//{
// rowIndex++;
// colIndex = 0;
// foreach (DataColumn col in table.Columns)
// {
// colIndex++;
// //sheet.Cells.AddValueCell(rowIndex, colIndex, row[col.ColumnName].ToString());//将数据添加到xls表格里
// Cell cell = cells.AddValueCell(rowIndex, colIndex, Convert.ToDouble(row[col.ColumnName].ToString()));//转换为数字型
// //如果你数据库里的数据都是数字的话 最好转换一下,不然导入到Excel里是以字符串形式显示。
// cell.Font.FontFamily = FontFamilies.Roman; //字体
// cell.Font.Bold = true; //字体为粗体
// }
//}
xls.Send();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现