一、.从NuGet中,引用 Microsoft.Office.Interop.Excel类库。
方法一、先打开一个execl表然后进行保存
Application xApp = null; Workbook xBook = null; ??????????try ???????????{ ???????????????//1.创建Applicaton对象 ???????????????xApp = new Application(); ???????????????xApp.Visible = true; ???????????????Workbooks wbs = xApp.Workbooks; ???????????????Workbook wb = wbs.Add(Missing.Value); ???????????????Worksheet xSheet = (Worksheet)wb.Worksheets["Sheet1"]; ???????????????Range r = xSheet.get_Range("A1", "N1"); ????????object[] objHeader = ???????????????{ ???????????????????"序号", "用户名", "手机号", "是否消费", "充值金额", "充值次数", "购买服务金额", "购买服务次数", "购买版权金额", "购买版权次数", "代金券金额", ???????????????????"赠送金额", "总金额", "账户余额" ???????????????????}; ??????????int ?row = 2;//第一行是标题 ???????????????for (int i = 0; i < array.Length; i++) ???????????????{ ???????????????????var str = array[i]; ???????????????????var list = TJsonResult.DeserializeObject<List<UserInfoAnalysisEntity>>(str); ???????????????????foreach (var entity in list) ???????????????????{ ???????????????????????if (entity.Cost == "是") ???????????????????????{ ???????????????????????????xSheet.Cells.Font.ColorIndex = 3; ???????????????????????} ???????????????????????else ???????????????????????{ ???????????????????????????xSheet.Cells.Font.ColorIndex = 1; ???????????????????????} ???????????????????????xSheet.Cells[row, 1] = entity.Id; ???????????????????????xSheet.Cells[row, 2] = entity.UserName; ???????????????????????xSheet.Cells[row, 3] = entity.Phone; ???????????????????????xSheet.Cells[row, 4] = entity.Cost; ???????????????????????xSheet.Cells[row, 5] = entity.Recharge; ???????????????????????xSheet.Cells[row, 6] = entity.RechargeNum; ???????????????????????xSheet.Cells[row, 7] = entity.BuyServices; ???????????????????????xSheet.Cells[row, 8] = entity.BuyServicesNum; ???????????????????????xSheet.Cells[row, 9] = entity.BuyCopyRight; ???????????????????????xSheet.Cells[row, 10] = entity.BuyCopyRightNum; ???????????????????????xSheet.Cells[row, 11] = entity.Coupon; ???????????????????????xSheet.Cells[row, 12] = entity.GiveMoney; ???????????????????????xSheet.Cells[row, 13] = entity.TotalMoney; ???????????????????????xSheet.Cells[row, 14] = entity.Balance; ???????????????????????row++;//表示行号 ???????????????????} ???????????????}
var filepath = AppDomain.CurrentDomain.BaseDirectory + "test123.xlsx";
xSheet.SaveAs(filepath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
更多操作:https://www.cnblogs.com/junshijie/p/5292087.html
.Net 操作Excel表格
原文地址:https://www.cnblogs.com/xiaoyaodijun/p/9336634.html