dylan-mo / excellibrary

Automatically exported from code.google.com/p/excellibrary
0 stars 0 forks source link

Error when setting a format and format breaking in excel. #74

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Using the code below I am getting errors with the formatting crashing at the 
same spot each time. 
At row 112 column D the formatting stops working. I get the error "file error. 
some number formats may have been lost." when I open the file. Going to sheet 4 
(names3) At line 89 the non string columns start getting random formats. On 
sheet 5 (names4) line 54 the format goes back to the formatting from the code. 
public void CreateWorkbook(DataSet dataset, List<ExcelFormat> excelFormats, 
string fileLocation)
        {
            var workbook = new Workbook();
            foreach (DataTable dt in dataset.Tables)
            {
                var worksheet = new Worksheet(dt.TableName);
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    worksheet.Cells[0, i] = new Cell(dt.Columns[i].ColumnName);

                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (excelFormats[i].CellFormat == "@")
                        {
                            worksheet.Cells[j + 1, i] = new Cell(dt.Rows[j][i].ToString());
                        }
                        else
                        {
                            var cell = new Cell(Convert.ToDouble(dt.Rows[j][i].ToString()))
                                           {
                                               Format = new CellFormat(CellFormatType.Custom, excelFormats[i].CellFormat)
                                           };
                            worksheet.Cells[j + 1, i] = cell;    
                        }

                    }
                }
                workbook.Worksheets.Add(worksheet);
            }
            workbook.Save(fileLocation);
        }

[TestMethod]
        public void FormatBuilder()
        {

            var builder = new ExcelCreator("c:\\test\\testformat.xls");
            var ds = new DataSet();
            for (int j = 0; j < 5; j++)
            {
                var table = new DataTable { TableName = "Names" +j };
                table.Columns.Add("FirstName");
                table.Columns.Add("LastName");
                table.Columns.Add("Birthdate");
                table.Columns.Add("Income", typeof(float));
                table.Columns.Add("Cost", typeof(float));
                for (int i = 0; i < 115; i++)
                {
                    DataRow row = table.NewRow();

                    row["FirstName"] = "John" + i;
                    row["LastName"] = "Smith" + i;
                    row["Birthdate"] = new DateTime(1980 + i, 01, 01).ToString("dd-MM-yyyy");
                    row["Income"] = i * 10000;
                    row["Cost"] = i * 100;
                    table.Rows.Add(row);

                }
                ds.Tables.Add(table);
            }

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?
dll = v2.0.50727 
VS 2010 C# .net 4.0
Windows 7 64 Enterprise
Office 2007
Please provide any additional information below.
The row/column where the formatting starts to error is always the same using 
this code so its not random. Also the location it starts working and starts to 
randomly format is always the same as well. 

Original issue reported on code.google.com by marc.irimia on 20 Oct 2010 at 6:31

Attachments:

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Opens fine in Open Office
Errors and closes in office 2003 when looking at a cell where the format is 
invalid. 

Original comment by marc.irimia on 20 Oct 2010 at 7:47