net导出excel文件同一行的数据如果在不同逻辑进行输入,会被覆盖掉的问题

问题:在导出数据成excel 的时候,会出现如果是同一行cell 的数据如果不是同时设定的时候,会出现数据会被覆盖掉

如图 红色框子中的会被覆盖掉

这个解决的办法为:在引入的npoi 中的方法涉及的部分。

    IRow Sheet1row1 = Sheet1.CreateRow(0);

而我们在逻辑处理完毕后,有的时候需要在左侧创建完毕后在进行创建右边的逻辑

这个时候,我们需要进行改变方法。

  IRow Sheet1row2 = Sheet1.GetRow(0);

这样我们创建的才不会被覆盖掉

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using Newtonsoft;
using Newtonsoft.Json;

using NPOI;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.SS.Formula.Eval;


using ICSharpCode.SharpZipLib.Zip;
using System.Data;
using System.Data.SqlClient;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
   public ActionResult DownLoadICBC(string DownLoadTime, string DownLoadType)
        {
            string WageTime = Convert.ToDateTime(DownLoadTime).ToString("yyyy-MM");
            Models.SalaryModel salary = new Models.SalaryModel();
            #region 0.表头

            // HSSFWorkbook XSSFWorkbook
            IWorkbook books = new HSSFWorkbook();//生成xlsx格式excel


            #endregion
            #region 分情况处理工行模板
            if (DownLoadType == "工行记录模板")
            {
                #region 工资处理单模板
                // 设置整体列宽
                // 固定高度
                ISheet Sheet1 = books.CreateSheet("Sheet1");

                #region 第1列
                IRow Sheet1row1 = Sheet1.CreateRow(0);
                Sheet1row1.Height = 10 * 150;//设置高度 75磅
                Sheet1.SetColumnWidth(0, 50 * 100);// 33 设置列宽
                Sheet1.SetColumnWidth(1, 70 * 100);// 33 设置列宽
                Sheet1.SetColumnWidth(2, 70 * 100);// 33 设置列宽
                Sheet1.SetColumnWidth(3, 50 * 100);// 33 设置列宽
                Sheet1.SetColumnWidth(4, 30 * 100);// 33 设置列宽
                Sheet1.SetColumnWidth(5, 30 * 100);// 33 设置列宽
                IFont ShowFont = books.CreateFont();
                // ShowFont.FontHeight = 10;
                // ShowFont.FontName = "Arial";
                // ShowFont.IsBold = true; // 加粗

                ICellStyle ShowTopStates = books.CreateCellStyle();
                ShowTopStates.FillForegroundColor = IndexedColors.Rose.Index;
                ShowTopStates.FillPattern = FillPattern.SolidForeground;
                ShowTopStates.SetFont(ShowFont);
                ShowTopStates.VerticalAlignment = VerticalAlignment.Center;// 头部居中
                ShowTopStates.BorderTop = BorderStyle.Thin;
                ShowTopStates.BorderRight = BorderStyle.Thin;
                ShowTopStates.BorderBottom = BorderStyle.Thin;
                ShowTopStates.BorderLeft = BorderStyle.Thin;

                ICellStyle ShowTopContent = books.CreateCellStyle();
                ShowTopContent.FillForegroundColor = IndexedColors.LightCornflowerBlue.Index;
                ShowTopContent.FillPattern = FillPattern.SolidForeground;
                ShowTopContent.SetFont(
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值