Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hello to everyone,
I have to do a Excel Manager with C# and i choose to use Gem Box Spreadsheet Free

C#
var ef = new ExcelFile();
            ef = ExcelFile.Load(File_Lettura);
            ExcelWorksheet ws = ef.Worksheets.ActiveWorksheet;

            int riga = 13;
            string s = (ws.Cells["B6"]).ToString();
            string[] r = s.Split('-');
            int c = 0;

            while (ws.Cells["B"+riga.ToString()].Value != null)
            {

                if (ws.Cells["F"+riga.ToString()].Value.ToString() != "")
                {
                    // add row
                    dgwFile.Rows.Add();

                    dgwFile.Rows[c].Cells[0].Value = r[0] + "-" + r[1] + "-" + ws.Cells["B"+riga.ToString()].Value.ToString();
                    dgwFile.Rows[c].Cells[1].Value = ws.Cells["D" + riga.ToString()].Value.ToString() + ws.Cells["G" + riga.ToString()].Value.ToString() + ws.Cells["H" + riga.ToString()].Value.ToString() + ws.Cells["I" + riga.ToString()].Value.ToString();
                    dgwFile.Rows[c].Cells[2].Value = ws.Cells["F" + riga.ToString()].Value.ToString();
                    dgwFile.Rows[c].Cells[3].Value = "0";
                    c++;
                }
                riga++;
            }


VS give me a problem at the first "IF" with error : An unhandled exception of type 'System.NullReferenceException' occurred.
I think the wrong rows are the first 3
thanks in advance :)
Posted

1 solution

Hi, if I understood you correctly you have a problem here:
C#
if (ws.Cells["F"+riga.ToString()].Value.ToString() != "")

Well the first thing that comes to my mind is that this cell is null, so try the following:
C#
ExcelCell cell = ws.Cells["F" + riga.ToString()];
if (cell != null && cell.Value.ToString() != "")
{
    // ...

Now to add some pointers to your code:

1. You don't have to initialize new ExcelFile instance if you are loading an existing excel file. So instead of this:
C#
var ef = new ExcelFile();
ef = ExcelFile.Load(File_Lettura);

Just use this:
C#
var ef = ExcelFile.Load(File_Lettura);


2. You can access each column, row and cell with a name by providing a string or with an index by providing an int.
In other words, for example instead of this:
C#
int riga = 13;
ws.Cells["B" + riga.ToString()]

You can use this:
C#
// Notice I reduced "riga" value by 1 because all indexes start with 0, while the row's names start with "1".
int riga = 12; 
ws.Columns["B"].Cells[riga];

3. Why are you using while loop?
It's much easier and intuitive to iterate through the required group of cell.
For example instead of this:
C#
while (ws.Cells["B"+riga.ToString()].Value != null)

Use something like this:
C#
foreach (ExcelCell cell in ws.Cells.GetSubrange("B13", "B" + ws.Rows.Count))
{
    if (cell == null || string.IsNullOrEmpty(cell.Value.ToString()))
        continue;

    // Do the rest ...
}


4. Last what is dgwFile, is it Windows Form's DataGridView control?
In case it is check out this sample.
It shows how to use DataGridViewConverter.ExportToDataGridView and DataGridViewConverter.ImportFromDataGridView methods.
 
Share this answer
 
Comments
Member 11822400 10-Jul-15 9:07am    
thanks :) i use while because i've never use foreach at school
Mario Z 10-Jul-15 13:38pm    
No problem, I'm glad to help you out.
Also in case you need any additional help with GemBox.Spreadsheet feel free to post your questions here on CodeProject.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900