我有Gem Box Spreadsheet msi的问题 [英] I have a problem with Gem Box Spreadsheet msi
问题描述
大家好,
我必须用C#做一个Excel Manager,我选择免费使用Gem Box Spreadsheet
< pre lang =cs> 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()]。值!= null )
{
< span class =code-keyword> if (ws.Cells [ F + riga.ToString()]。Value.ToString()!= )
{
// 添加行
dgwFile.Rows.Add();
dgwFile.Rows [c] .Cells [ 0 ]。值= r [ 0 ] + - + r [ 1 ] + - + ws.Cells [ B + riga.ToString()]。Value.ToString();
dgwFile.Rows [c] .Cells [ 1 ]。值= ws.Cells [ D + riga.ToString()]。Value.ToString()+ ws.Cells [ G + riga.ToString()]。Value.ToString()+ ws.Cells [ < span class =code-string> H + riga.ToString()]。Value.ToString()+ ws.Cells [ I + riga.ToString()]。Value.ToString();
dgwFile.Rows [c] .Cells [ 2 ]。值= ws.Cells [ F + riga.ToString()]。Value.ToString();
dgwFile.Rows [c] .Cells [ 3 ]。值= 0\" 跨度>;
c ++;
}
riga ++;
}
VS在第一个IF给我一个问题,有错误:'System'类型的未处理异常。发生了NullReferenceException。
我认为错误的行是第3个
提前感谢:)如果我理解你的话你有问题:
if (ws.Cells [ F + riga.ToString()]。Value.ToString()!= )
我的第一件事就是请注意,此单元格 null
,请尝试以下操作:
ExcelCell cell = ws.Cells [< span class =code-string> F + riga.ToString()];
if (cell!= null && cell.Value.ToString()! = )
{
// ...
现在添加一些指向代码的指针:
1.如果要加载现有的Excel文件,则不必初始化新的 ExcelFile
实例。所以不是这样:
var ef = new ExcelFile();
ef = ExcelFile.Load(File_Lettura);
只需使用:
var ef = ExcelFile.Load(File_Lettura);
2.您可以访问通过提供字符串
或通过提供 int
提供索引的每个列,行和单元格的名称。 />
换句话说,例如,而不是:
int riga = < span class =code-digit> 13 ;
ws.Cells [ B + riga.ToString()]
您可以使用:
// 注意我将riga值减1,因为所有索引都以0开头,而行的名称以1开头。
int riga = 12 ;
ws.Columns [ B]。Cells [riga];
3.为什么在循环时使用?
循环使用它更加容易和直观所需的一组单元格。
例如,而不是:
而 (ws.Cells [ B + riga.ToString()]。值!= null )
使用类似这样的内容:
< span class =code-keyword> foreach (ExcelCell cell in ws.Cells.GetSubrange( B13, B + ws.Rows.Count))
{
if (cell == null || string .Is NullOrEmpty(cell.Value.ToString()))
continue ;
// 其余的......
}
4.最后是什么 dgwFile
,它是Windows Form的DataGridView控件吗? br />
如果查看此样本。
它显示了如何使用 DataGridViewConverter.ExportToDataGridView
和 DataGridViewConverter.ImportFromDataGridView
方法。
Hello to everyone,
I have to do a Excel Manager with C# and i choose to use Gem Box Spreadsheet Free
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 :)
Hi, if I understood you correctly you have a problem here:
if (ws.Cells["F"+riga.ToString()].Value.ToString() != "")
Well the first thing that comes to my mind is that this cell isnull
, so try the following:
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 newExcelFile
instance if you are loading an existing excel file. So instead of this:
var ef = new ExcelFile(); ef = ExcelFile.Load(File_Lettura);
Just use this:
var ef = ExcelFile.Load(File_Lettura);
2. You can access each column, row and cell with a name by providing astring
or with an index by providing anint
.
In other words, for example instead of this:
int riga = 13; ws.Cells["B" + riga.ToString()]
You can use this:
// 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 usingwhile
loop?
It's much easier and intuitive to iterate through the required group of cell.
For example instead of this:
while (ws.Cells["B"+riga.ToString()].Value != null)
Use something like this:
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 isdgwFile
, is it Windows Form's DataGridView control?
In case it is check out this sample.
It shows how to useDataGridViewConverter.ExportToDataGridView
andDataGridViewConverter.ImportFromDataGridView
methods.
这篇关于我有Gem Box Spreadsheet msi的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!