C#调用epplus 创建Excel

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:00   1852   0

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();

}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP