有关myxls的编程

          MyXls是用C#开源项目,可以应用于asp.net 或者 .net应用程序上。它根据微软公开的Excle文档格式文件(BIFF),以二进制格式直接生成excel文档,支持Excel versions 97 - 2007. 。这意味着可以不用在服务器上安装office就能够以excle格式输出数据库中存储的数据了。这对于许多项目来说都是很有用的。

    目前MyXls已经实现了单元格(cell)的格式设置,包括文本颜色、文本大小、字体、单位格边框、底色、列宽、行高,合并单元格,多个sheet页等功能。
    目前MyXls还不支持在excel文档中生成对象(如、文本框、按钮等)。MyXls主页称即将实现对excel文件的读取功能,个人认为读取的功能的用处还不是很多。
    MyXls主页: http://myxls.in2bits.org/wiki/MainPage.ashx

    下载页面:http://myxls.in2bits.org/wiki/Downloads.ashx

 

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Data.SqlClient;
  11. using org.in2bits.MyXls;
  12. public partial class _Default : System.Web.UI.Page 
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         if (!this.IsPostBack)
  17.         {
  18.             BindData();
  19.         }
  20.     }
  21.     public void BindData()
  22.     {
  23.         gridPolice.DataSource = GetPoliceMan();
  24.         gridPolice.DataBind();
  25.         gridCar.DataSource = GetCar();
  26.         gridCar.DataBind();
  27.     }
  28.     public string GetConnectionString()
  29.     {
  30.         return ConfigurationManager.AppSettings["ConnectionString"].ToString();
  31.     }
  32.     public DataTable GetPoliceMan()
  33.     {
  34.         SqlConnection con = new SqlConnection(GetConnectionString());
  35.         SqlDataAdapter da = new SqlDataAdapter("select * from tblPolice", con);
  36.         DataTable dt = new DataTable();
  37.         da.Fill(dt);
  38.         return dt;
  39.     }
  40.     public DataTable GetCarByPoliceID(int ID)
  41.     {
  42.         string strSql =string.Format("select * from tblCar where ID={0}",ID); 
  43.         SqlConnection con = new SqlConnection(GetConnectionString());
  44.         SqlDataAdapter da = new SqlDataAdapter(strSql, con);
  45.         DataTable dt = new DataTable();
  46.         da.Fill(dt);
  47.         return dt;
  48.     }
  49.     public DataTable GetCar()
  50.     {
  51.         SqlConnection con = new SqlConnection(GetConnectionString());
  52.         SqlDataAdapter da = new SqlDataAdapter("select * from tblCar", con);
  53.         DataTable dt = new DataTable();
  54.         da.Fill(dt);
  55.         return dt;
  56.     }
  57.     protected void btnCreateExcel_Click(object sender, EventArgs e)
  58.     {
  59.         //System.Data.DataTable exceldt = new System.Data.DataTable();
  60.         //exceldt = Session["Reports"] as System.Data.DataTable;
  61.         XlsDocument xls = new XlsDocument();
  62.         xls.FileName =  "Demo.xls";
  63.         //Add file Attribute
  64.         xls.SummaryInformation.Author = "Tim Erickson"//author
  65.         xls.SummaryInformation.Subject = "A wacky display of Excel file generation";
  66.         xls.DocumentSummaryInformation.Company = "in2bits.org";
  67.         int rowMin = 3;
  68.         int colMin = 1;
  69.         //获取警员资料记录
  70.         DataTable dtPolice = GetPoliceMan();
  71.         DataTable dtCar = GetCar();
  72.         Worksheet sheet = xls.Workbook.Worksheets.AddNamed("Demo");
  73.         MergeArea ma = new MergeArea(1, 2, 1, dtCar.Columns.Count);
  74.         
  75.         sheet.AddMergeArea(ma);
  76.         Cells cells = sheet.Cells;
  77.         Cell cellTitle = cells.Add(1, 1, "湖南省交警大队报表");
  78.         
  79.         //设置边框
  80.         //cellTitle.UseBorder = true;
  81.         //cellTitle.TopLineColor = Colors.Red;
  82.         //cellTitle.TopLineStyle = 5;
  83.         //cellTitle.BottomLineColor = Colors.Red;
  84.         //cellTitle.BottomLineStyle = 5;
  85.         //文字居中
  86.         cellTitle.HorizontalAlignment = HorizontalAlignments.Centered;
  87.         cellTitle.VerticalAlignment = VerticalAlignments.Centered;
  88.         cellTitle.Font.Bold = true;
  89.         //cellTitle.Pattern = 1;
  90.         //cellTitle.PatternBackgroundColor = Colors.White;//填充的底色
  91.         //cellTitle.PatternColor = Colors.White;//设定填充线条的颜色
  92.         //设置列属性
  93.         ColumnInfo cinfo = new ColumnInfo(xls, sheet);
  94.         cinfo.Collapsed = true;
  95.         cinfo.ColumnIndexStart =(ushort)colMin;
  96.         cinfo.ColumnIndexEnd = (ushort)(dtCar.Columns.Count-1);
  97.         cinfo.Collapsed = true;
  98.         
  99.         cinfo.Width = 90*60;
  100.         sheet.AddColumnInfo(cinfo);
  101.         //警员资料
  102.         int num = 0;
  103.         for (int i = 0; i < dtPolice.Rows.Count ; i++)
  104.         {
  105.             int policeID = Convert.ToInt32(dtPolice.Rows[i]["ID"].ToString());
  106.             DataTable dtCarByID = GetCarByPoliceID(policeID);
  107.             
  108.             for (int r = 0; r < (dtCarByID.Rows.Count + 1); r++)
  109.             {
  110.                 if (r == 0)
  111.                 {
  112.                     //添加列名:
  113.                     for (int c = 0; c < dtCar.Columns.Count; c++)
  114.                     {
  115.                         Cell cellHeader = cells.Add(rowMin + r, colMin + c, dtCar.Columns[c].ColumnName);
  116.                         cellHeader.Font.Bold = true;
  117.                         cellHeader.HorizontalAlignment = HorizontalAlignments.Centered;
  118.                         cellHeader.Pattern = 1;
  119.                         cellHeader.PatternColor = Colors.Silver;
  120.                         cellHeader.UseBorder = true;
  121.                         cellHeader.TopLineStyle = 1;
  122.                         cellHeader.TopLineColor = Colors.Silver;
  123.                         cellHeader.BottomLineStyle = 1;
  124.                         cellHeader.BottomLineColor = Colors.Silver;
  125.                         cellHeader.LeftLineStyle = 1;
  126.                         cellHeader.LeftLineColor = Colors.Silver;
  127.                         cellHeader.RightLineStyle = 1;
  128.                         cellHeader.RightLineColor = Colors.Silver;
  129.                         
  130.                     }
  131.                 }
  132.                 else
  133.                 {
  134.                     for (int c = 0; c < dtCar.Columns.Count; c++)
  135.                     {
  136.                         Cell cellBody = cells.Add(rowMin + r, colMin + c, dtCarByID.Rows[r + 1 - 2][c].ToString());
  137.                         cellBody.UseBorder = true;
  138.                         cellBody.TopLineStyle = 1;
  139.                         cellBody.TopLineColor = Colors.Silver;
  140.                         cellBody.BottomLineStyle = 1;
  141.                         cellBody.BottomLineColor = Colors.Silver;
  142.                         cellBody.LeftLineStyle = 1;
  143.                         cellBody.LeftLineColor = Colors.Silver;
  144.                         cellBody.RightLineStyle = 1;
  145.                         cellBody.RightLineColor = Colors.Silver;
  146.                     }
  147.                 }
  148.             }
  149.             //控制行数
  150.             num += dtCarByID.Rows.Count + 2;
  151.             //控制行数
  152.             rowMin += dtCarByID.Rows.Count + 2;
  153.             //添加所属警察
  154.             ma = new MergeArea(num + 2, num + 2, 1, dtCar.Columns.Count);
  155.             sheet.AddMergeArea(ma);
  156.             Cell cellPoliceMan = cells.Add(num + 2, 1, dtPolice.Rows[i]["PoliceName"].ToString() +"  " +dtPolice.Rows[i]["Position"].ToString());
  157.             cellPoliceMan.Font.Bold = true;
  158.         }
  159.             xls.Send();
  160.     }
  161. }

效果图:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值