'ExcelWorksheet' does not contain a definition for 'Dimension' - excel

I get this error and I don't know why:
'ExcelWorksheet' does not contain a definition for 'Dimension' and no accessible extension method 'Dimension' accepting a first argument of type 'ExcelWorksheet' could be found
(are you missing a using directive or an assembly reference?)
var file = new FileInfo(#"C:\test.xlsx");
// Ustvarjanje objekta ExcelPackage
using (var package = new ExcelPackage(file))
{
// Dostop do lista delovnega lista
var workSheet = package.Workbook.Worksheets[1];
// Ustvarjanje objekta DataTable
DataTable table = new DataTable();
// Prebiranje vseh stolpcev iz delovnega lista
for (int col = 1; col <= workSheet.Dimension.End.Column; col++)
{
// Ustvarjanje stolpca z imenom
table.Columns.Add(workSheet.Cells[1, col].Value.ToString());
}
// Prebiranje vseh vrstic iz delovnega lista
for (int row = 2; row <= workSheet.Dimension.End.Row; row++)
{
// Ustvarjanje nove vrstice
DataRow dataRow = table.NewRow();
// Prebiranje vseh stolpcev iz delovnega lista
for (int col = 1; col <= workSheet.Dimension.End.Column; col++)
{
// Dodajanje vrednosti v vrstico
dataRow[col - 1] = workSheet.Cells[row, col].Value.ToString();
}
// Dodajanje vrstice v tabelo
table.Rows.Add(dataRow);
}
// Pretvorba objekta DataTable v json
var json = JsonConvert.SerializeObject(table, Formatting.Indented);
// Izpis rezultata
Console.WriteLine(json);
Console.ReadLine();
}
}
I am using
net7.0-windows

Related

The ExcelPackage object does not return sheets

I am trying to upload an excel file to a hosted Blazor webassembly application, for which I am using the following code:
string path= #"D:\Otros\LibrosExcel\ReferenciasDotación.xls";
FileInfo fileInfo = new FileInfo(path);
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (ExcelPackage excelPackage = new OfficeOpenXml.ExcelPackage(fileInfo))
{
//loop all worksheets
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.FirstOrDefault();
//loop all rows
for (int i = 1; i <= worksheet.Dimension.End.Row; i++)
{
//loop all columns in a row
for (int j = 1; j <= worksheet.Dimension.End.Column; j++)
{
//add the cell data to the List
if (worksheet.Cells[i, j].Value != null)
{
excelData.Add(worksheet.Cells[i, j].Value.ToString());
}
}
}
}
return excelData;
but the line of code
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.FirstOrDefault ();
returns null

How to get schedule element data in revit using C#

I am new to Revit API and am working in C#. I want to get the schedule element parameters value using C#. I used the below code to get the view schedule.
var viewSchedule = new FilteredElementCollector(document)
.OfClass(typeof(ViewSchedule))
.FirstOrDefault(e => e.Name == "MyScheduleName") as ViewSchedule;
Schedule Element Data
From the above schedule, I used the below code to get the element data (please refer the above screenshot link) but it taking long time to reflect the output (10 to 15 seconds).
var rowCount = viewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfRows;
var colCount = viewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfColumns;
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
data += viewSchedule.GetCellText(SectionType.Body, i, j);
}
}
Please let me know is there any alternate approach to get the schedule data using C#.
Thanks in advance.
Maybe you can also use ViewSchedule.Export as demonstrated by The Building Coder discussing The Schedule API and Access to Schedule Data.
Yes, you can easily access Schedule data without exporting.
Firstly, get all the schedules and read the data cell by cell. Secondly, create dictionary and store data in form of key, value pairs. Now you can use the schedule data as you want. I have tried this in Revit 2019.
Here is the implementation.
public void getScheduleData(Document doc)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
IList<Element> collection = collector.OfClass(typeof(ViewSchedule)).ToElements();
String prompt = "ScheduleData :";
prompt += Environment.NewLine;
foreach (Element e in collection)
{
ViewSchedule viewSchedule = e as ViewSchedule;
TableData table = viewSchedule.GetTableData();
TableSectionData section = table.GetSectionData(SectionType.Body);
int nRows = section.NumberOfRows;
int nColumns = section.NumberOfColumns;
if (nRows > 1)
{
//valueData.Add(viewSchedule.Name);
List<List<string>> scheduleData = new List<List<string>>();
for (int i = 0; i < nRows; i++)
{
List<string> rowData = new List<string>();
for (int j = 0; j < nColumns; j++)
{
rowData.Add(viewSchedule.GetCellText(SectionType.Body, i, j));
}
scheduleData.Add(rowData);
}
List<string> columnData = scheduleData[0];
scheduleData.RemoveAt(0);
DataMapping(columnData, scheduleData);
}
}
}
public static void DataMapping(List<string> keyData, List<List<string>>valueData)
{
List<Dictionary<string, string>> items= new List<Dictionary<string, string>>();
string prompt = "Key/Value";
prompt += Environment.NewLine;
foreach (List<string> list in valueData)
{
for (int key=0, value =0 ; key< keyData.Count && value< list.Count; key++,value++)
{
Dictionary<string, string> newItem = new Dictionary<string, string>();
string k = keyData[key];
string v = list[value];
newItem.Add(k, v);
items.Add(newItem);
}
}
foreach (Dictionary<string, string> item in items)
{
foreach (KeyValuePair<string, string> kvp in item)
{
prompt += "Key: " + kvp.Key + ",Value: " + kvp.Value;
prompt += Environment.NewLine;
}
}
Autodesk.Revit.UI.TaskDialog.Show("Revit", prompt);
}

reading Excel with ClosedXML

What would be the most efficient way to read an entire Excel file using ClosedXML and returning List<List<object>> ?
This somehow doesn't give me data. I get empty lists.
var wb = new XLWorkbook(finalFilePath);
var ws = wb.Worksheets.First();
var range = ws.RangeUsed();
var colCount = range.ColumnCount();
var rowCount = range.RowCount();
var i = 1;
var j = 1;
List<List<object>> data = new List<List<object>>();
while (i < rowCount + 1)
{
List<object> row = new List<object>();
while (j < colCount + 1)
{
row.Add(ws.Cell(i, j).Value);
j++;
}
data.Add(row);
i++;
}
This gets the job done:
Dictionary<Tuple<int, int>, object> data = new Dictionary<Tuple<int, int>, object>();
using (XLWorkbook wb = new XLWorkbook(filePath))
{
var ws = wb.Worksheets.First();
var range = ws.RangeUsed();
for (int i = 1; i < range.RowCount() + 1; i++)
{
for (int j = 1; j < range.ColumnCount() + 1; j++)
{
data.Add(new Tuple<int, int>(i,j), ws.Cell(i,j).Value);
}
}
}

How to make parent child relationship in C1flexgrid

I am using C1Flexgrid and I need to make parent child relation in this grid. But child details need to show in same grid (no other grid ) and when I clicked on + expand should happen and vice versa.
I have written below code where I am having one column in datatable related to parent and child . If it is parent then I am making it 1 else 0.
When I tried with this code. R2 row is coming as child node of r which should not be a case as it is parent node.
Please help me on this .
private void Form3_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable("customers");
dt.Columns.Add("abc");
dt.Columns.Add("ddd");
dt.Columns.Add("eee");
dt.Columns.Add("parent");
var r = dt.NewRow();
r["abc"] = "11";
r["ddd"] = "12";
r["eee"] = "13";
r["parent"] = "1";
var r1 = dt.NewRow();
r1["ddd"] = "12";
r1["eee"] = "14";
r1["parent"] = "0";
var r2 = dt.NewRow();
r2["abc"] = "11";
r2["ddd"] = "1222";
r2["eee"] = "14";
r2["parent"] = "1";
var rr32 = dt.NewRow();
rr32["abc"] = "11";
rr32["ddd"] = "1222";
rr32["eee"] = "14";
rr32["parent"] = "0";
dt.Rows.Add(r);
dt.Rows.Add(r1);
dt.Rows.Add(r2);
dt.Rows.Add(rr32);
grid1.DataSource = dt;
GroupBy("parent", 1);
// show outline tree
grid1.Tree.Column = 2;
// autosize to accommodate tree
grid1.AutoSizeCol(grid1.Tree.Column);
grid1.Tree.Show(1);
}
void GroupBy(string columnName, int level)
{
object current = null;
for (int r = grid1.Rows.Fixed; r < grid1.Rows.Count; r++)
{
if (!grid1.Rows[r].IsNode)
{
var value = grid1[r, columnName];
string value2 = grid1[r, "parent"].ToString();
if (!object.Equals(value, current))
{
// value changed: insert node, apply style
if (value2.Equals("0"))
{
grid1.Rows.InsertNode(r, level);
grid1.Rows[r].Style = _nodeStyle[Math.Min(level, _nodeStyle.Length - 1)];
r++;
}
// show group name in first scrollable column
//grid1[r, grid1.Cols.Fixed+1] = value;
// update current value
current = value;
}
}
}
}
}
Your code was almost there, i have manipulated GroupBy method to fit your need. It solves your current requirement but you have to handle sorting and other functionalists of grid yourself.
Hope this helps!
void GroupBy(string columnName, int level)
{
object current = null;
for (int r = grid1.Rows.Fixed; r < grid1.Rows.Count; r++)
{
if (!grid1.Rows[r].IsNode)
{
var value = grid1[r, columnName];
if (!object.Equals(value, current))
{
// value changed: insert node, apply style
grid1.Rows.InsertNode(r, level);
grid1.Rows[r].Style = _nodeStyle[Math.Min(level, _nodeStyle.Length - 1)];
// show group name in first scrollable column
Row row = grid1.Rows[r + 1];
for (int i = 0; i < grid1.Cols.Count; i++)
{
grid1[r, i] = row[i];
}
grid1.Rows[r + 1].Visible = false;
r++;
// update current value
current = value;
}
}
}
}

Handle Null Value In Gridview During Exporting To Excel

I am working with a C# Windows application. Trying to export data from gridview to Excel , but when the gridview column is empty I do get error message.
How to handle that? Please help
This is my code
// Store Header from Gridview to Excel
for (int i = 1; i < dgvresult.Columns.Count + 1; i++)
{
Excel.Cells[1, i] = dgvresult.Columns[i - 1].HeaderText;
}
// Loop rows and columns of Gridview to store to Excel
for (int i = 0; i < dgvresult.Rows.Count; i++)
{
for (int j = 0; j < dgvresult.Columns.Count; j++)
{
Excel.Cells[i + 2, j + 1] = dgvresult.Rows[i].Cells[j].Value.ToString(); // Here when the value in Gridview is empty error how to handle this
}
}
Excel.ActiveWorkbook.SaveCopyAs("D:\\Asserts.xls");
Excel.ActiveWorkbook.Saved = true;
Excel.Quit();
MessageBox.Show("Excel file created,you can find the file D:\\Asserts.xls");
Excel.Visible = true;
Found solution do a checking by code below
private void btnexport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.ApplicationClass Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Excel.Application.Workbooks.Add(Type.Missing);
Excel.Columns.ColumnWidth = 14;
// Store Header from Gridview to Excel
for (int i = 1; i < dgvresult.Columns.Count + 1; i++)
{
Excel.Cells[1, i] = dgvresult.Columns[i - 1].HeaderText;
}
// Loop rows and columns of Gridview to store to Excel
for (int i = 0; i < dgvresult.Rows.Count; i++)
{
for (int j = 0; j < dgvresult.Columns.Count; j++)
{
if (dgvresult.Rows[i].Cells[j].Value == null)
{
dgvresult.Rows[i].Cells[j].Value = "NA"; // Where the gridview is empty do a checking and Insert NA
}
Excel.Cells[i + 2, j + 1] = dgvresult.Rows[i].Cells[j].Value.ToString();
}
}
Excel.ActiveWorkbook.SaveCopyAs("D:\\Asserts.xls");
Excel.ActiveWorkbook.Saved = true;
Excel.Quit();
MessageBox.Show("Excel file created,you can find the file D:\\Asserts.xls");
Excel.Visible = true;
}

Resources