Excel.Application excel = null;
Excel.Workbooks wbs = null;
Excel.Workbook wb = null;
Excel.Worksheet ws = null;
Excel.Range range1 = null;
object Nothing = System.Reflection.Missing.Value;
try
{
excel = new Excel.Application();
excel.UserControl = true;
excel.DisplayAlerts = false;
excel.Application.Workbooks.Open(this.FilePath,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing ) ;
wbs = excel.Workbooks;
wb = wbs[1];
ws = (Excel.Worksheet)wb.Worksheets["Sheet2"];
int rowCount = ws.UsedRange.Rows.Count;
int colCount = ws.UsedRange.Columns.Count;
if (rowCount <= 0)
throw new InvalidFormatException("文件中没有数据记录");
if (colCount < 4 )
throw new InvalidFormatException("字段个数不对");
for (int i = 0;i { this.rowNo = i + 1;
object[] row = new object[4];
for (int j = 0;j<4;j++)
{
range1 = ws.get_Range(ws.Cells[i+2,j+1],ws.Cells[i+2,j+1]);
row[j] = range1.Value; if (row[0] == null)
{
this.isNullRecord++;
break;
}
}
if (this.isNullRecord > 0)
continue; DataRow dataRow = this.readExcel(row); if (this.isNullRecord == 1)
continue;
if (this.verifyData(dataRow) == false)
errFlag++;
this.updateTableCurr(dataRow);
}
}
finally
{
if (excel != null)
{
if (wbs != null)
{
if (wb != null)
{
if (ws != null)
{
if (range1 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(range1);
range1 = null;
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws = null;
}
wb.Close(false,Nothing,Nothing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
}
wbs.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
wbs = null;
}
excel.Application.Workbooks.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
GC.Collect();
}
}
在网上看到一段代码,其中throw new InvalidFormatException("");
this.isNullRecord
(this.verifyData(dataRow) == false)
这几句什么意思c#excel
Excel.Workbooks wbs = null;
Excel.Workbook wb = null;
Excel.Worksheet ws = null;
Excel.Range range1 = null;
object Nothing = System.Reflection.Missing.Value;
try
{
excel = new Excel.Application();
excel.UserControl = true;
excel.DisplayAlerts = false;
excel.Application.Workbooks.Open(this.FilePath,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing ) ;
wbs = excel.Workbooks;
wb = wbs[1];
ws = (Excel.Worksheet)wb.Worksheets["Sheet2"];
int rowCount = ws.UsedRange.Rows.Count;
int colCount = ws.UsedRange.Columns.Count;
if (rowCount <= 0)
throw new InvalidFormatException("文件中没有数据记录");
if (colCount < 4 )
throw new InvalidFormatException("字段个数不对");
for (int i = 0;i { this.rowNo = i + 1;
object[] row = new object[4];
for (int j = 0;j<4;j++)
{
range1 = ws.get_Range(ws.Cells[i+2,j+1],ws.Cells[i+2,j+1]);
row[j] = range1.Value; if (row[0] == null)
{
this.isNullRecord++;
break;
}
}
if (this.isNullRecord > 0)
continue; DataRow dataRow = this.readExcel(row); if (this.isNullRecord == 1)
continue;
if (this.verifyData(dataRow) == false)
errFlag++;
this.updateTableCurr(dataRow);
}
}
finally
{
if (excel != null)
{
if (wbs != null)
{
if (wb != null)
{
if (ws != null)
{
if (range1 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(range1);
range1 = null;
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws = null;
}
wb.Close(false,Nothing,Nothing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
}
wbs.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
wbs = null;
}
excel.Application.Workbooks.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
GC.Collect();
}
}
在网上看到一段代码,其中throw new InvalidFormatException("");
this.isNullRecord
(this.verifyData(dataRow) == false)
这几句什么意思c#excel
解决方案 »
- 实现记录投票者信息
- [Help]Oracle中Number/Data类型数据如何格式转换及存储空值问题。。。
- 一上传到服务器后,本来本地是中文的报表就变成英文的了?
- 使用微软工具的版权费用问题
- SqlDataAdapter事务处理发生问题。。
- 谁有关于串口通信的完整的类呢???(求救了!!!)
- c#的客户端能不能调用WebService定义的全局变量啊?如果能怎样调用啦?
- 问个在WPF中的TextBox中提取文本内容的小问题 求大家帮忙
- dotnetfx35 和 winfrom程序如何集成到一起啊?
- 请问哪里有c#的电子图书下载?
- 如何从WC对象实例中得到WCF服务器地址
- C#怎么获取QQ头像及等级?
isNullRecord 空格数据的个数
verifyData 通过将指定的签名数据与为指定数据计算的签名进行比较来验证指定的签名数据。
InvalidFormatException 在哪个命名空间
object[] row = new object[4];
for (int j = 0;j<4;j++)
{
range1 = ws.get_Range(ws.Cells[i+2,j+1],ws.Cells[i+2,j+1]);
row[j] = range1.Value; if (row[0] == null)
这一句是什么意思