最近做NPOI导出EXCEL,之前给边框加粗线。        for (int i = 0; i <= 1; i++)
            {
                for(int j = 0; j <= 2; j++)
                {
                    s.GetRow(i).GetCell(j).CellStyle = style;
                }
                
            }结果报错了,引发的异常:“System.NullReferenceException”我就单独做一个,for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 5; j++)
                {
                    Console.Write("{i}:{j}", i , j);
                    
                }
                Console.WriteLine();
            }结果还是报错,
“System.FormatException”类型的未经处理的异常在 mscorlib.dll 中发生 其他信息: 输入字符串的格式不正确。
不知怎么写才正确,指教一下,谢谢。

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    for (int i = 0; i <= 2; i++)
                    {
                        for (int j = 0; j <= 5; j++)
                        {
                            Console.Write("i是{0},j是{1}", i, j);
                            Console.Write("\n ");                    }
                        Console.WriteLine();                }
                    Console.ReadLine();            }
                catch (Exception ex)
                {
                    
                    throw;
                }
               
            }
        }
    }
      

  2.   

     s.GetRow(i).GetCell(j).CellStyle = style;第一段代码,i 和 j ,这个怎么解决呢???
      

  3.   

    https://www.cnblogs.com/siyunianhua/p/6852750.html 
    https://www.cnblogs.com/luowende2012/archive/2012/07/28/2612848.html
      

  4.   

    sheet.Cells[i , j]
      

  5.   

    private void button1_Click(object sender, EventArgs e)
            {
                Form1 f = new Form1();            ISheet s = w.CreateSheet("a");
                w.CreateSheet("b");
                w.CreateSheet("c");            IRow r= s.CreateRow(1);            ICell cell= r.CreateCell(1);
                
                ICellStyle style = w.CreateCellStyle();
                style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thick;
                style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;            ICellStyle cel = gets();            s.CreateRow(0).CreateCell(0).SetCellValue("测试1");
                s.GetRow(0).CreateCell(1).SetCellValue("测试2");
                s.GetRow(0).CreateCell(2).SetCellValue(textBox2.Text);            
                s.CreateRow(1).CreateCell(0).SetCellValue("测试222");
                s.CreateRow(1).CreateCell(1).SetCellValue(Convert.ToDouble(textBox1.Text));
                s.GetRow(1).CreateCell(2).SetCellValue(Convert.ToDouble(textBox2.Text));
      
                for (int i = 0; i <= 1; i++)
                {
                    for(int j = 0; j <= 2; j++)
                    {
                        s.GetRow(i).GetCell(j).CellStyle = style;
                    }
                    
                }

                s.CreateRow(2).CreateCell(1).SetCellValue(f.textBox1.Text);
                s.GetRow(2).CreateCell(2).SetCellValue(f.textBox2.Text);
                s.GetRow(2).CreateCell(3).SetCellValue(label5.Text);
       
                FileStream file = new FileStream(@"test.xls", FileMode.Create);            w.Write(file);            file.Close();
            }