none
Excel write problem RRS feed

  • Question

  • I am trying to display an Excel file from my dataset, I got still errors, something like: wrong parameters. What's wrong with this code? The statment or mayby types wrong?

    int Col;
    
    Col = 0;
                    foreach (DataRow dr in dataset.Tables[0].Rows)
                    {
                        if ((Convert.ToInt32(Procs.RetDbl(dr[1]))) != 999 && (Convert.ToInt32(Procs.RetDbl(dr[2]))) != 999 && (Convert.ToInt32(Procs.RetDbl(dr[3]))) != 999)
                        {
                            xlWorkSheet.Cells["B" + (Col + 13)] = Procs.RetStr(dr[0]);
                            xlWorkSheet.Cells["C" + (Col + 13)] = ((Convert.ToInt32(Procs.RetDbl(dr[2]))) == 999) ? "" : (Procs.RetDbl(dr[2], " "));
                            xlWorkSheet.Cells["D" + (Col + 13)] = ((Convert.ToInt32(Procs.RetDbl(dr[3]))) == 999) ? "" : (Procs.RetDbl(dr[3], " "));
                            xlWorkSheet.Cells["E" + (Col + 13)] = ((Convert.ToInt32(Procs.RetDbl(dr[1]))) == 999) ? "" : (Procs.RetDbl(dr[1], " "));
                            Col++;
                        }
                    }
    

    This is my function Procs.RetDbl - but it's good i use it a long time:

    static public object RetDbl(object var, object def = null, object rnd = null)
    		{
    			object returnValue;
    			
    			if (Information.IsNumeric(var))
    			{
    				if (rnd == null)
    				{
    					returnValue = System.Convert.ToDouble(var);
    				}
    				else
    				{
    					returnValue = Math.Round(System.Convert.ToDouble(var), System.Convert.ToInt32(rnd));
    				}
    			}
    			else
    			{
    				if (def == null)
    				{
    					returnValue = 0;
    				}
    				else
    				{
    					returnValue = def;
    				}
    			}
    			
    			return returnValue;
    		}
    


     

    Monday, October 17, 2011 12:00 PM

Answers

  • Hi Jimmy,

     

    Thanks for posting in the MSDN Forum.

     

    It’s based on my experience that you have error parameters at this line “xlWorkSheet.Cells[“B”+(Conl+13)]=Procs.RetStr(dr[0]);”. As far as I know, you can use integer index to declare the specific range. However you aren’t able to mix integer index and string together. According to your snippet I suppose that you want to set value to the specific range use this sentence “xlWorkSheet.Range[“B”+string.Format(“{0:d}”,Col+13)].Value = Procs.RetStr(dr[0]);”.

     

    If you have any questions, please feel free to let me know.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 19, 2011 2:59 AM
    Moderator