首页 新闻 会员 周边

NOPI 遍历Excel修改背景色问题

0
悬赏园豆:80 [待解决问题]

用NOPI 读取一个已经存在的Excel ,现在遍历Excel,根据某些判断修改单元格的背景色。为什么遍历后的表格破损,而且只能保存最后操作设置的背景色。关键代码如下:

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
HSSFWorkbook myworkbook = new HSSFWorkbook(FileToStream(openFileDialog.FileName));
sheetName = myworkbook.GetSheetName(1);
mysheet = (HSSFSheet)myworkbook.GetSheet(sheetName);
sheetNum = mysheet.LastRowNum;

 


for (int i = 1; i <= mysheet.LastRowNum; i++)
{

   if()//判断

  { 

ICellStyle style = myworkbook.CreateCellStyle();//样式
//设置背景色
style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Green.Index;
style.FillPattern = FillPattern.SolidForeground;
style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Green.Index;
mysheet.CreateRow(i).CreateCell(m).CellStyle = style;

FileStream fs1 = File.OpenWrite(openFileDialog.FileName);
myworkbook.Write(fs1); //向打开的这个xls文件中写入表并保存。
fs1.Close();

  }

else

{

ICellStyle style = myworkbook.CreateCellStyle();//样式
//设置背景色
style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
style.FillPattern = FillPattern.SolidForeground;
style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
mysheet.CreateRow(i).CreateCell(m).CellStyle = style;

FileStream fs1 = File.OpenWrite(openFileDialog.FileName);
myworkbook.Write(fs1); //向打开的这个xls文件中写入表并保存。
fs1.Close();

}

}

}

这种方法居然把我的Excel表格原有的数据丢失了,纠结了好久,单独设置某一个单元格背景是可以的,但是遇到设置多个单元格背景时候就报错了,是不是我的Excel 保存写法不对,总觉得

FileStream fs1 = File.OpenWrite(openFileDialog.FileName);
myworkbook.Write(fs1); //向打开的这个xls文件中写入表并保存。 
fs1.Close();

这段有问题,可是找不出原因。请大侠赐教,在线等候。

每天在路上的主页 每天在路上 | 初学一级 | 园豆:6
提问于:2014-12-23 16:29
< >
分享
所有回答(2)
0

哪里报错?你跟了吗?

版本是最新吗?

隔壁老王来了 | 园豆:99 (初学一级) | 2014-12-23 17:23

跟踪过了,条件里面不同的样式设置代码也都执行过了,但是前一次执行的为什么没有保存,好将有些数据丢失了。

支持(0) 反对(0) 每天在路上 | 园豆:6 (初学一级) | 2014-12-23 17:26

@每天在路上: 你要跟报错点在哪里,确保逻辑没问题吧

支持(0) 反对(0) 隔壁老王来了 | 园豆:99 (初学一级) | 2014-12-23 17:34

没有报错,就是执行结果不对。代码没有报错

支持(0) 反对(0) 每天在路上 | 园豆:6 (初学一级) | 2014-12-23 19:57

@每天在路上: 你试下把那坨代码换成

 using (FileStream fs = new FileStream(enFileDialog.FileName, FileMode.OpenOrCreate, FileAccess.Write))
                 {
                    myworkbook.Write(fs);
                    }

支持(0) 反对(0) 隔壁老王来了 | 园豆:99 (初学一级) | 2014-12-23 20:10

解决了,还是对NOPI理解不够,就是一句话的问题。mysheet.CreateRow(i).CreateCell(m).CellStyle = style;

换成row.Cells[m].CellStyle = style;
就好了。不该是CreateRow,这样就把原来的行数据丢失了。

支持(0) 反对(0) 每天在路上 | 园豆:6 (初学一级) | 2014-12-23 20:43

@每天在路上: 恩恩,恭喜!!

支持(0) 反对(0) 隔壁老王来了 | 园豆:99 (初学一级) | 2014-12-23 20:44
0
  1 /// <summary>
  2     /// excel 序列化帮助类
  3     /// 使用说明:给要序列化的对象属性加上此描述
  4     /// 【System.ComponentModel.DescriptionAttribute】 templpl=》 [Description("姓名")]
  5     /// 帮助类会自动反射出列名
  6     /// 
  7     /// 1》调用 AddSheetTitle 添加excel表头 var plist=  AddSheetTitle(typeof(People),sheet);
  8     /// 2》调用 LoadingData 装载数据 LoadingData(sheet,People[] list,plist);
  9     /// 3》调用 FinishingWidth(ISheet iSheet)自适应整理excel的列宽度
 10     /// 4》完毕
 11     /// 
 12     ///  AddSheetTitle 的(Type type) 和LoadingData 的 (object[] list)是同一对象
 13     ///  
 14     /// 易纪元 hz
 15     /// </summary>
 16     public class ExcelSerializationByNPIO
 17     {
 18 
 19         /// <summary>
 20         /// 创建表头 得到一个表头顺序
 21         /// </summary>
 22         /// <param name="type"></param>
 23         /// <param name="iSheet"></param>
 24         /// <returns></returns>
 25         public static List<PropertyInfo> AddSheetTitle(Type type, ISheet iSheet, ICellStyle style = null)
 26         {
 27             var newListprops = new List<PropertyInfo>();
 28             var listprops = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
 29             IRow rowTitle = iSheet.CreateRow(0);
 30             for (int i = 0; i < listprops.Length; i++)
 31             {
 32                 var atr = Attribute.GetCustomAttribute(listprops[i], typeof(System.ComponentModel.DescriptionAttribute));
 33                 if (atr == null)
 34                 {
 35                     continue;
 36                 }
 37                 string des = ((System.ComponentModel.DescriptionAttribute)atr).Description;
 38                 if (string.IsNullOrEmpty(des))
 39                 {
 40                     continue;
 41                 }
 42                 ICell icell = rowTitle.CreateCell(i);
 43                 icell.SetCellValue(des);
 44                 if (style != null)
 45                 {
 46                     icell.CellStyle= style;
 47                 }
 48                 newListprops.Add(listprops[i]);
 49                 iSheet.SetColumnWidth(i, 200 * 256);
 50             }
 51             return newListprops;
 52         }
 53 
 54 
 55         /// <summary>
 56         /// 装填数据
 57         /// </summary>
 58         /// <param name="iSheet"></param>
 59         /// <param name="list"></param>
 60         /// <param name="props">创建头返回的顺序</param>
 61         /// <param name="startindex">起始值,</param>
 62         /// <returns></returns>
 63         public static bool LoadingData(ISheet iSheet, object[] list, PropertyInfo[] props, int startindex = 1, ICellStyle style = null)
 64         {
 65             for (int i = 0; i < list.Length; i++)
 66             {
 67                 IRow temprow = iSheet.CreateRow(startindex);
 68                 startindex++;
 69                 for (int j = 0; j < props.Length; j++)
 70                 {
 71                     ICell icell = temprow.CreateCell(j);
 72                     icell.SetCellValue(TypeToString(props[j].GetValue(list[i])));
 73                     if (style != null)
 74                     {
 75                         icell.CellStyle = style;
 76                     }
 77                 }
 78             }
 79             return true;
 80         }
 81 
 82         /// <summary>
 83         /// 该方法会自适应整理excel的列宽度,宽度为每行该列最大值
 84         /// </summary>
 85         /// <param name="iSheet"></param>
 86         public static void FinishingWidth(ISheet iSheet)
 87         {
 88             var arrayDicColumW = new Dictionary<int, int>();
 89             var ie = iSheet.GetRowEnumerator();
 90             while (ie.MoveNext())
 91             {
 92                 var row = (IRow)ie.Current;
 93                 var rowcells = row.Cells;
 94                 for (int i = 0; i < rowcells.Count; i++)
 95                 {
 96                     int length = System.Text.Encoding.Default.GetBytes(rowcells[i].ToString()).Length;
 97                     if (arrayDicColumW.ContainsKey(i))
 98                     {
 99                         int temp = arrayDicColumW[i];
100                         if (length > temp)
101                             arrayDicColumW[i] = length;
102                     }
103                     else
104                     {
105                         arrayDicColumW[i] = length;
106                     }
107                 }
108             }
109             foreach (var item in arrayDicColumW)
110             {
111                 iSheet.SetColumnWidth(item.Key, (item.Value + 1) * 256);
112             }
113 
114         }
115 
116         public static string TypeToString(object obj)
117         {
118             if (obj == null)
119                 return "";
120             return obj.ToString();//mark 
121         }
122     }
0xc | 园豆:237 (菜鸟二级) | 2014-12-26 15:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册