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<rowCount;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; }
}
.......
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<rowCount;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; }
}
.......
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货