为什么在 C# 中读取 excel 的相同代码不再读取 excel?

  • 本文关键字:excel 读取 代码 不再 c# excel
  • 更新时间 :
  • 英文 :


我正在为 Revit 编写一个插件,C#代码长时间准确地读取 excel 单元格。然后对于同一个脚本,我给出了另一个 excel 文件的路径,现在它没有excel文件,即使是它长时间读取的第一个 excel 文件。我不知道为什么从excel读取单元格的相同脚本不再起作用。我收到一个例外,因为;"数组的偏移量和长度超出范围,或者计数大于从索引到源集合末尾的元素数",因为在那一行,我试图从列表中提取一些项目,但实际上该列表什么都没有,因为它没有从 excel 中获取值您可以在链接中看到图像。在此处输入图像描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Structure;
using Microsoft.Office.Interop.Excel;
using Line = Autodesk.Revit.DB.Line;
using System.IO;
namespace RevitComm
{
[TransactionAttribute(TransactionMode.Manual)]
class Boundaries : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

//-------------------------------------------------Reading from excel  coordinates-------------------------------------------------------------|
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:Example.xlsx");
Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;
//---------------------------------------------------Rows and columns--------------------------------------------------------------------------|
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
//--------------------------------------------------Conversion Factor--------------------------------------------------------------------------|                                                 
double cF = 1 / 0.3048;
//-------------------------------------- No of entered rows + 1 as rows starts from 2 -------------------------------------------------------|                                         
int rows = Convert.ToInt32(xlRange.Cells[3, "B"].value2) + 3;

//---------------------------------------------------Getting Active Revit Document--------------------------------------------------------------|
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;

double z = 0;

//------------------------------------------- Getting the level on which Wall starts------------------------------------------------------------|
Level level = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_Levels)
.WhereElementIsNotElementType()
.Cast<Level>()
.First(x => x.Name == "Level 1");
//------------------------------------------------- Getting level on which Roof to be placed ----------------------------------------------------|
Level upperlevel = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_Levels)
.WhereElementIsNotElementType()
.Cast<Level>()
.First(x => x.Name == "Level 2");

//-------------------------------------------------  Roof type  ---------------------------------------------------------------------------------|
var rooftype = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Roofs)
.WhereElementIsElementType().Cast<RoofType>().First(x => x.Name == "Generic - 9"");


//------------- Floor type example __________//
var floorty = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Floors)
.WhereElementIsElementType().Cast<FloorType>().First(x => x.Name == "OurFloor");


//----- wall type example
var wallty = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls)
.WhereElementIsElementType().Cast<WallType>().First(x => x.Name == "JustGeneric");

//------------------------------------------ List for Points -----------------------------------------------------------------------------|
List<XYZ> pointStr = new List<XYZ>();

//----------Start Points------/          
for (int i = 4; i < rows; i = i + 2)
{
XYZ pi = new XYZ(xlRange.Cells[i, "N"].value2 * cF, xlRange.Cells[i, "O"].value2 * cF, z);
pointStr.Add(pi);
}

//----------------End Points ------------/
List<XYZ> pointEnd = new List<XYZ>();
for (int i = 5; i <= rows; i = i + 2)
{
XYZ pi = new XYZ(xlRange.Cells[i, "N"].value2 * cF, xlRange.Cells[i, "O"].value2 * cF, z);
pointEnd.Add(pi);
}


//-----------------------------------------------------------List for Lines -------------------------------------------------------|

List<Line> lines = new List<Line>();
for (int i = 0; i < (rows - 3) / 2; i++)
{
XYZ pk = pointStr.ElementAt(i);
XYZ pj = pointEnd.ElementAt(i);
Line l = Line.CreateBound(pk, pj);
lines.Add(l);
}
//------------------------------------------------------- List for Curves for walls ------------------------------------------------------|
List<Curve> curWalls = new List<Curve>();
for (int i = 0; i < (rows - 3) / 2; i++)
{
Line li = lines.ElementAt(i);
curWalls.Add(li);
}
List<Curve> Curvet = curWalls.GetRange(0,4);
CurveLoop crvloopExE = CurveLoop.Create(Curvet);
CurveLoop offcrExE = CurveLoop.CreateViaOffset(crvloopExE, 0.5 * cF, new XYZ(0, 0, 1));
CurveArray curArrExE = new CurveArray();
foreach (Curve c in offcrExE)
{
/// To put the curves to Currve array
curArrExE.Append(c);
}


// ------------------------------------ Start transaction -------------------------------------------------------|
try
{
//return Result.Succeeded;
using (Transaction trans = new Transaction(doc, "Neus-Haus"))
{
trans.Start();
// Creating Walls

for (int i = 0; i <= 5; i++)
Wall W1 = Wall.Create(doc, curWalls.ElementAt(i), wallty.Id, level.Id, 3 * cF, 0, false, false);
}


trans.Commit();
}
return Result.Succeeded;
}
catch (Exception e)
{
message = e.Message;
return Result.Failed;
}

}
}
}

也许需要修改文件的路径,以便每个''都变得"\"以与Windows操作系统兼容。

然后对于同一个脚本,我给出了另一个 excel 文件的路径,现在它没有采用 excel 文件

这可能是问题的关键,而您没有给我们太多信息(代码应该发布而不是作为图像发布,这种方式对我们来说更容易测试(。尝试以管理员身份执行程序,如果您从Visual Studio运行它,然后以管理员身份执行Visual Studio,则很可能缺少程序运行所需的权限。我的答案是基于您的代码已经工作的前提

最新更新