问题:在导出数据成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(ShowFont);
ShowTopContent.VerticalAlignment = VerticalAlignment.Center;// 头部居中