C#调用epplus 创建Excel

1.下载Epplus. http://epplus.codeplex.com/

2.添加引用Epplus.dll.




protected void btnToExcel_Click(object sender, EventArgs e)

    {


        OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.ExcelPackage();
        OfficeOpenXml.ExcelWorkbook wb = ep.Workbook;
        OfficeOpenXml.ExcelWorksheet ws = wb.Worksheets.Add("data");
        //配置文件属性  
        wb.Properties.Category = "类别";
        wb.Properties.Author = "作者";
        wb.Properties.Comments = "备注";
        wb.Properties.Company = "公司";
        wb.Properties.Keywords = "关键字";
        wb.Properties.Manager = "管理者";
        wb.Properties.Status = "内容状态";
        wb.Properties.Subject = "主题";
        wb.Properties.Title = "标题";
        wb.Properties.LastModifiedBy = "最后一次保存者";




        ws.Cells["A1"].Value = "ID";
        ws.Cells["B1"].Value = "SlaveID";
        ws.Cells["C1"].Value = "TyName";
        ws.Cells["D1"].Value = "DamName";
        ws.Cells["E1"].Value = "HumName";
        ws.Cells["F1"].Value = "TempName";


        ws.Cells["G1"].Value = "CommID";
        ws.Cells["H1"].Value = "SYSTime";
        ws.Cells["I1"].Value = "Temp_Ct";
        ws.Cells["J1"].Value = "Temp_Wb";
        ws.Cells["K1"].Value = "Temp_Bf";


        ws.Cells["L1"].Value = "Rl_hum";
        ws.Cells["M1"].Value = "Th_ps";
        ws.Cells["N1"].Value = "Set_Ct";
        ws.Cells["O1"].Value = "Set_Rlhum";


        ws.Cells["P1"].Value = "Set_Wb";
        ws.Cells["Q1"].Value = "Set_Bf";
        ws.Cells["R1"].Value = "Fault_EggTr";
        ws.Cells["S1"].Value = "Fault_Fan";
        ws.Cells["T1"].Value = "Fan_Close";


        ws.Cells["U1"].Value = "Fault_Dp";
        ws.Cells["V1"].Value = "Alarm_LT";
        ws.Cells["W1"].Value = "Alarm_HT";
        ws.Cells["X1"].Value = "Alarm_LH";
        ws.Cells["Y1"].Value = "Alarm_HH";


        ws.Cells["Z1"].Value = "Alarm_LBf";
        ws.Cells["AA1"].Value = "Alarm_HBf";
        ws.Cells["AB1"].Value = "Alarm_TestT";
        ws.Cells["AC1"].Value = "Rv_ph";
        ws.Cells["AD1"].Value = "Fault_PbWb";


        ws.Cells["AE1"].Value = "Fault_PbCt";
        ws.Cells["AF1"].Value = "Fault_PbBf";
        ws.Cells["AG1"].Value = "Ban_EggTr";
        ws.Cells["AH1"].Value = "Alarm_CCB";
        ws.Cells["AI1"].Value = "OverLoad_Fan";


        ws.Cells["AJ1"].Value = "Door_open";
        ws.Cells["AK1"].Value = "JP2_2";
        ws.Cells["AL1"].Value = "JP2_3";
        ws.Cells["AM1"].Value = "JP2_4";
        ws.Cells["AN1"].Value = "JP2_5";


        ws.Cells["AO1"].Value = "JP2_6";
        ws.Cells["AP1"].Value = "JP3_2";
        ws.Cells["AQ1"].Value = "JP3_3";
        ws.Cells["AR1"].Value = "JP3_4";
        ws.Cells["AS1"].Value = "JP3_5";


        ws.Cells["AT1"].Value = "JP3_6";
        ws.Cells["AU1"].Value = "JP6_1";
        ws.Cells["AV1"].Value = "JP6_2";


        for (int i = 1; i < 49; i++)
        {
            ws.Cells[1, i].Style.Fill.PatternType = ExcelFillStyle.Solid;
            ws.Cells[1, i].Style.Fill.BackgroundColor.SetColor(Color.LightGray);
        }
        string fileName;
        string sid = SlaveNum.SelectedValue;//Request.QueryString["SID"];
        string sName;
        string sql;
        DataTable dt = null;
        sql = "select * from [SlaveInfo] where SlaveID=" + sid;


        dt = DataExecSqlHelper.ExecuteQuerySql(sql);
        if (dt == null || dt.Rows.Count == 0) return;
        DataRow dd = dt.Rows[0];


        sName = dd["SlaveName"].ToString();


        


        fileName = sid + "_" + srvTime_Start.Value + ".xlsx";
      


        dt = List_Bll.GetAllDataList("ListData", sid, srvTime_Start.Value, srvTime_End.Value);




        for (Int32 i = 1; i < dt.Rows.Count+1; i++)
        {
            for (int j=1;j<49;j++){
                ws.Cells[i + 1, j].Value = dt.Rows[i-1][j-1];
            }
        }


            //写到客户端(下载)  
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "");
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.BinaryWrite(ep.GetAsByteArray());
      
        Response.Flush();
        Response.End();
        
    }
Epplus 简介:Epplus是一个使用Open Office XML(Xlsx)文件格式,能读写Excel 2007/2010文件的开源组件 功效:支持对excel文档的汇入汇出,图表(excel自带的图表基本都可以实现)的列印 使用:首先应该下载Epplus的dll文件 1> 添加dll文件至工程bin文件中 2>在程式中添加引用 using OfficeOpenXml; using OfficeOpenXml.Drawing; using OfficeOpenXml.Drawing.Chart; using OfficeOpenXml.Style; 3>所有的操作语句需要放置在下面的using中 using (ExcelPackage package = new ExcelPackage()) { } 4.添加新的sheet var worksheet = package.Workbook.Worksheets.Add(“sheet1"); 5.单元格赋值,这里多说一句,NPOI必须先创建单元格,然后再给单元格赋值,而Epplus不需要,直接找到单元格进行赋值就可以了. worksheet.Cells[int row, int col].Value = “”; 或者 worksheet.Cells["A1"].Value = “”; 6.合并单元格 worksheet.Cells[int fromRow, fromCol, int toRow,int toCol].Merge = true; 7.获取某一个区域 var rangeData= worksheet.Cells[fromRow, fromCol, toRow, toCol]; 8.设置字体 worksheet.Cells.Style.Font.Name= “正楷”; worksheet.Cells.Style.Font.Color worksheet.Cells.Style.Font.Size 9.设置边框的属性 worksheet.Cells.Style.Border.Left.Style= ExcelBorderStyle.Thin ; worksheet.Cells.Style.Border.Right.Style= ExcelBorderStyle.Thin; worksheet.Cells.Style.Border.Top.Style= ExcelBorderStyle.Thin; worksheet.Cells.Style.Border.Bottom.Style= ExcelBorderStyle.Thin; 10.对齐方式 worksheet.Cells.Style.HorizontalAlignment=ExcelHorizontalAlignment.Center; worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom; 11. 设置整个sheet的背景色 worksheet.Cells.Style.Fill.PatternType= ExcelFillStyle.Solid; worksheet.Cells.Style.Fill.BackgroundColor.SetColor(Color.LightBlue); 12.折行显示 worksheet.Cells.Style.WrapText= true; 13.单元格自动适应大小 worksheet.Cells.Style.ShrinkToFit= true; 14.格式化单元格value值 worksheet.Cells.Style.Numberformat.Format= "0.00"; 15.锁定 worksheet.Cells["A1"].Style.Locked= true; 注:此处锁定某一个单元格的时候,只有在整个sheet被锁定的情况下才可以被锁定,不然加上锁定属性也是不起作用的~~ 二.Epplus另一个出色的地方就是支持图表的列印.功能的實現很簡單,難點在于需求比較細的點上,epplus可能不好實現,但是總的來說是比較好的一個列印圖表的工具 1.简单介绍一下可以实现的图表类型: 直條圖、折綫圖、圓形圖、橫條圖、散佈圖、區域圖 等類型的圖表 2.使用:分为三步, 第一步是将需要显示在图表中的 数据列印到excel中. 第二步是创建所需要的图表类型(折线图为例) var chart = (worksheet.Drawings.AddChart("LineChart", eChartType.Line) as ExcelLineChart); 第三步为图表添加第一步列印的数据区间就可以了 chart.Series.Add(Y軸顯示的數據源,X軸顯示的數據源) 3.图表的功能就这样实现了,很简单吧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值