分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

.Net 操作Excel表格

发布时间:2023-09-06 02:04责任编辑:董明明关键词:暂无标签

一、.从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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved