一 操作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();
}
}
}
149

被折叠的 条评论
为什么被折叠?



