Click here to Skip to main content
15,887,392 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
SQL
 Hell all, I am doing an ExcelAddin in VisualStudio 2010 for Excel 2007. In my Excel Workbook I have a named range that I call MyRange. It goes from cells C10 to M25.The other cells contain data that I am not interestet in. How can I read the data from only the cells that have value in them within MyRange? I do not want to read the empty cells. I then want to take these values and copy them into a table in Word.

I have tried to use UsedRange but that reads everything from A1-M25 ( I only want to read the cells with value from C10-M25). Here is what I got so far.


C#
string FileName = @"C:\MyFile.xlsx";
Excel.Application xlApp = xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;

xlWorkBook = xlApp.Workbooks.Open(FileName);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

object cell1 = "C10", cell2 ="M25";
//Here are some different versions that I tried. I also tried to use the SpecialCell method //but it doesn´t seem to work. Maybe I don´t know how to?
Excel.Range namedRange = (Excel.Range)xlWorkSheet.get_Range("C10", "M25");
Excel.Range last = (Excel.Range)xlWorkSheet.UsedRange;
Excel.Range usedRange = (Excel.Range)xlWorkSheet.get_Range("C10", last);


Then I have made a Word table to write the data into.

C#
table = doc.Tables.Add(wrdRng, MyRange.Rows.Count,MyRange.Columns.Count, ref oMissing, ref oMissing);

         for (int rCnt = 1; rCnt <= MyRange.Rows.Count; rCnt++)
            {
               for (int cCnt = 1; cCnt <= MyRange.Columns.Count; cCnt++)
                   {

                      string str = (string)(MyRange.Cells[rCnt, cCnt] as Excel.Range).Value2.ToString();
                      table.Cell(rCnt, cCnt).Range.Text = str;

            // I am not sure about this last bit but it works when I have limited the Range to only the active cells
            //but as soon as it finds a cell with null it throws an exception

                    }
            }


All help is highly appreciated, thank you all.
Posted

1 solution

Your code looks quite ok.

string str = (string)(MyRange.Cells[rCnt, cCnt] as Excel.Range).Value2.ToString();
The exception is probably in the above line.

Dont type case directly into string.
Instead either use the null coalesce operator (??), use Convert.ToString() or directly check for null.
The reason is if you try and cast a null into string an exceltion will be thrown.

Here is an example of what you could do -
string str = = (MyRange.Cells[rCnt, cCnt] as Excel.Range).Value2 ?? string.empty);<br />
<br />
You could also try<br />
string str = (MyRange.Cells[rCnt, cCnt] as Excel.Range).Value2 != string.empty ? MyRange.Cells[rCnt, cCnt] as Excel.Range).Value2.toString() : string.empty;<br />
<br />
Give one of these a try and check if you still get this error.
 
Share this answer
 
Comments
SteiniEy 12-Jul-12 13:46pm    
Thank you Abhinav, I will have a try. My main problem though is reading only the cells with values within my predifined range (MyRange). Do you have any suggestions on that?
SteiniEy 13-Jul-12 11:27am    
The suggestion you gave me about didn´t compile, I got it to compile by taking out one of the = and also I added one bracket. It still gives me and exception, as soon as I try to copy a value from a cell that has no value it throws an a exception.

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