使用C Sharp向Excel插入数据为空 [英] Data insert to Excel using C sharp is null

查看:183
本文介绍了使用C Sharp向Excel插入数据为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据插入excel单元格.我不是从单元格A1中插入,而是插入例如range("G3","H8").但是我的数据为空.谁能帮忙.
这是我的代码

I tried to insert data to excel cells. I was not insert from cell A1, but I insert for example range("G3","H8"). However my data was null. Can anybody help.
Here is my code

int colCount = rng.Columns.Count;
                int rowCount = rng.Rows.Count;

                string[,] strData = new string[rowCount, colCount];
                int r = 1, c = 3;

                for (r = 1; r < rowCount; r++)
                {
                    strData[r, colCount - 2] = "First Data";
                    strData[r, colCount - 1] = "Second Data";
                    //strData[r, 2] = "Third Data";

                    //get range

                }
                object startCell = xlWorkSheet.Cells[2, 7];
                object endCell = xlWorkSheet.Cells[rowCount, colCount];
                rng = xlWorkSheet.get_Range(startCell, endCell);
                rng.Value2 = strData;




当我调试时,rng.Value2 = strData;为空.

谢谢,




When I debug, rng.Value2 = strData; is null.

thanks,

推荐答案

否,strData不为null,因为您在显示的代码的第三行中对其进行了初始化.看起来更彻底.

但是,您没有使用索引0初始化行,因为数组从0开始索引,但是循环从1开始.您应该从r = 0开始.此外,不要使用外部作用域中的loop变量(绝大部分,永远不要使用).而是在循环语句中声明它:
No, strData is not null, as you initialized it in third line of the code you show. Look more thoroughly.

However, you did not initialize the row with the index 0, because arrays are indexed with from 0, but your loop starts with 1. You should start from r = 0. Besides, don''t use the loop variable from outside scope (pretty much, never ever). Instead, declare it in the loop statement:
for (int r = 0; r < rowCount; ++r) { /* ... */ }


另外,不要给变量名r这样简短的名称.您是否在考虑需要搜索某些代码或对其进行修改的时间?

希望这些说明能使您解决其余问题.

祝你好运,

—SA


Also, don''t give so short variable names as r. Do you think about the time when you need to search for some code or modify it?

I hope these notes will let you to fix the rest.

Good luck,

—SA


这篇关于使用C Sharp向Excel插入数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆