zoukankan      html  css  js  c++  java
  • .Net 操作Excel表格

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

    另一种方法是引用 Free Spire.XLS for .NET 这个类库 

    https://www.cnblogs.com/Yesi/category/649975.html

  • 相关阅读:
    iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
    iOS开发UI篇—简单介绍静态单元格的使用
    jade反编译
    jade复用
    sublime修改TAB缩进
    jade模板引擎的基本使用
    MongoDB直接执行js脚本
    初识MongoDB
    php练习:每5个商品一排
    MongoDB聚合查询
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/9336634.html
Copyright © 2011-2022 走看看