NPOI 1.2.1 for .NET 1.1: Error on SetColumnWidth

Aug 4, 2011 at 3:48 PM

Hi!

I'm using NPOI 1.2.1 for .NET 1.1, and when I call SetColumnWidth() method, an error occurs!

check the message:

Specified cast is not valid.
at System.Convert.ToInt32(Object value)\r\n   at NPOI.HSSF.Record.Aggregates.ColumnInfoRecordsAggregate.SetColumnInfoFields(ColumnInfoRecord ci, NullableInt16 xfStyle, NullableInt32 width, NullableInt32 level, NullableBoolean hidden, NullableBoolean collapsed)\r\n   at NPOI.HSSF.Record.Aggregates.ColumnInfoRecordsAggregate.SetColumn(Int32 targetColumnIx, NullableInt16 xfIndex, NullableInt32 width, NullableInt32 level, NullableBoolean hidden, NullableBoolean collapsed)\r\n   at NPOI.HSSF.Model.Sheet.SetColumn(Int32 column, NullableInt16 xfStyle, NullableInt32 width, NullableInt32 level, NullableBoolean hidden, NullableBoolean collapsed)\r\n   at NPOI.HSSF.Model.Sheet.SetColumnWidth(Int32 column, Int32 width)\r\n   at NPOI.HSSF.UserModel.HSSFSheet.SetColumnWidth(Int32 column, Int32 width)\r\n   at ConsoleApplication1.Class1.Main(String[] args) in c:\\projects\\consoleapplication1\\class1.cs:line 64

now, check my code:

using System;
using System.IO;
using NPOI.HSSF.UserModel;

namespace ConsoleApplication1
{
	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			FileStream v_file;

			HSSFWorkbook v_wb; 

			HSSFSheet v_sheet;

			int v_row_index;

			HSSFRow v_row;

			HSSFCell v_cell;

			HSSFFont v_font_header;

			HSSFCellStyle v_style_header;

			try
			{
				v_wb = new HSSFWorkbook();

				v_sheet = v_wb.CreateSheet("Employees");

				v_font_header = v_wb.CreateFont();

				v_font_header.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;

				v_style_header = v_wb.CreateCellStyle();

				v_style_header.SetFont(v_font_header);

				v_row_index = 0;

				v_row = v_sheet.CreateRow(v_row_index);

				v_cell = v_row.CreateCell(0);

				v_cell.SetCellValue("ID");

				v_cell.CellStyle = v_style_header;

				v_cell = v_row.CreateCell(1);

				v_cell.SetCellValue("Name sdsdsd sd sd sd s");

				v_cell.CellStyle = v_style_header;

				v_cell = v_row.CreateCell(2);

				v_cell.SetCellValue("E-mail");

				v_cell.CellStyle = v_style_header;

				v_sheet.SetColumnWidth(0, 50);
				v_sheet.SetColumnWidth(1, 50);
				v_sheet.SetColumnWidth(2, 50);

				v_file = new FileStream("employees.xls", FileMode.Create);

				v_wb.Write(v_file);

				v_file.Close();
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex.Message + " - " + ex.StackTrace);
				Console.ReadLine();
			}
		}
	}
}

tks!

Coordinator
Aug 23, 2011 at 11:58 PM

NPOI for .NET 1.1 is no longer supported. However, if you have budget for technical support, the NPOI team can help on that.

 

Sep 11, 2012 at 3:36 PM

Hi, You can fix it changing line 420 of NPOI.HSSF.Record.Aggregates.ColumnInfoRecordsAggregate to ci.ColumnWidth = Convert.ToInt32(width.Value);

I hope it would be useful for someone.