when I make an AJAX call to receive the JSON-object defined in this c# code I have to 46+ seconds to receive this object. The object itself is only 12kb large. Is it because of my c# code(which doesn't take that long to execute?) of is it something else. Im testing it on my localhost IIS server.
This is my code:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult LoadAudit(int id, string sheet)
{
FactsCustomerRepository<Cateringaudit> repo = new FactsCustomerRepository<Cateringaudit>();
//IQueryable<Cateringaudit> result = repo.GetAll(i => i.State == 1);
Cateringaudit result1 = repo.Get(id);
WorkBook wb = new WorkBook();
wb.read(new MemoryStream(result1.ExcelData));
object[] jsonObjects = new object[3];
//sheetnames to collect data from
string[] sheetNames = { "contract", "proces", "output" };
//itterate trough all sheets in excel file
for (int sheetCount = 0; sheetCount < sheetNames.Length; sheetCount++)
{
wb.Sheet = wb.findSheetByName(sheetNames[sheetCount]);
//Create new array with same lenght as rows with data
Dictionary<string, string[]> excelData = new Dictionary<string, string[]>();
//iterate trough all rows in worksheet
for (int i = 1; i < wb.LastRow + 2; i++)
{
excelData.Add(blabla);
jsonObjects[sheetCount] = excelData;
}
}
return Json(jsonObjects);
}
Going on a limb here: opening Excelsheets in C# is ridiculously slow. There are great libraries out there which are way faster:
EEPLUS: http://epplus.codeplex.com/
But to rule out Excel first: have you tried returning a static JsonObject and not use excel?
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult LoadAudit(int id, string sheet)
{
return Json(); // something like this
}
Related
The goal is to fill excel cells with elements names. Using EPPlus.
_elementsDB gives just "Autodesk.Revit.DB.Wall".
int col = 1;
for ( int row=2; row < _elementsDB.Count; row++ )
{
ws.Cells[row, col].Value = _elementsDB;
}
Tying to populate either an array or list. Nothing works.
FilteredElementCollector collector = new FilteredElementCollector(doc);
IList<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements();
List<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements();
//First Option
string[] _elementsNameArr = new string[] { };
foreach (Element e in _elementsDB)
{
_elementsNameArr = e.Name;
}
//Second Option
List <string> _elementsNameList = new List<string>;
foreach (Element e in _elementsDB)
{
_elementsNameList = e.Name;
}
Also tried to create a sorted list, didn't work either. Shows up an excxeption System.Argument.Exception "A record with such a key already exists".
SortedList<string, Element> _elementNameSorted = new SortedList<string, Element>();
foreach (Element e in _elementsDB)
{
_elementNameSorted.Add(e.Name,e);
}
When you use the method .ToElements() it returns a IList<Element> that you can convert it later, you cannot directly assign its result to List<Element>, you have to use LINQ to convert, using .ToElements().ToList()
If you don't have it yet, be sure to add using System.Linq; to the top of your code.
Anyway, there's no need to convert to List<Element> here, try the code below:
FilteredElementCollector collector = new FilteredElementCollector(doc);
IList<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements();
//List<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements(); // <---- IF YOU ALREADY DECLARED _elementsDB BEFORE, YOU CAN'T HAVE THIS HERE TOGETHER
List <string> _elementsNameList = new List<string>(); // <-- YOU'VE MISSED () HERE
foreach (Element e in _elementsDB)
{
_elementsNameList.Add(e.Name); // <-- HERE YOU HAVE TO ADD IT TO THE LIST, NOT ASSIGN TO THE LIST, YOU CANNOT ASSIGN A string TO A List<string>, YOU HAVE TO ADD
}
//Sorting your list would be
_elementsNameList = _elementsNameList.OrderBy(q => q).ToList();
//...... Write here if you have any code before writing the Excel
try
{
WriteXLS("YOU EXCEL FILE PATH HERE", "YOUR WORKSEET NAME HERE", _elementsNameList, 2); // <--- METHOD SOWN IN THE CODE BELOW
}
catch(Exception e)
{
TaskDialog.Show("Error", e.Message);
}
For writing to an existing Excel file you can use the method bellow:
private void WriteXLS(string filePath, string workSheetName, List<string> elementsNamesList, int startRow = 1, int col = 1)
{
FileInfo existingFile = new FileInfo(filePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
ExcelWorksheet ws = GetWorkSheet(package, workSheetName);
int maxRows = elementsNamesList.Count;
for (int row = startRow; row <= maxRows; row++)
{
ws.Cells[row, col].Value = elementsNamesList[row];
}
}
}
And, before running it, be sure to have you Excel file closed, it doesn't work if the file is opened.
I'm using NPOI to export data into excel.
So I created a List that will pull data from my database.
Now My question is how can I read my list data and write the data on my excel Sheet.
The following is my part of my code:
IWorkbook workbook;
workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
ISheet excelSheet = workbook.CreateSheet("Candidates");
IRow row = excelSheet.CreateRow(0);
foreach (var data in ApplicationList)
{
}
workbook.Write(fs);
So basically I need help on foreach (var data in ApplicationList)
While writing data cells can be created and SetCellValue can help set the data.
Below I have tried to iterate over a single column and list of strings.
This works fine on my system.
IWorkbook workbook = new HSSFWorkbook();
ISheet excelSheet = workbook.CreateSheet("Candidates");
IRow row = excelSheet.CreateRow(0);
var applicantList = new List<string> { "David", "Paul" };
var excelColumns = new[] { "Name" };
IRow headerRow = excelSheet.CreateRow(0);
var headerColumn = 0;
excelColumns.ToList().ForEach(excelColumn =>
{
var cell = headerRow.CreateCell(headerColumn);
cell.SetCellValue(excelColumn);
headerColumn++;
});
var rowCount = 1;
applicantList.ForEach(applicant => {
var row = excelSheet.CreateRow(rowCount);
var cellCount = 0;
excelColumns.ToList().ForEach(column => {
var cell = row.CreateCell(cellCount);
cell.SetCellValue(applicant);
cellCount++;
});
rowCount++;
});
var stream = new MemoryStream();
workbook.Write(stream);
string FilePath = "/Users/hemkumar/hem.xls"; //path to download
FileStream file = new FileStream(FilePath, FileMode.CreateNew,
FileAccess.Write);
stream.WriteTo(file);
file.Close();
stream.Close();
I hope it helps.
I know I am a little late here but I think it may help others
I have developed an excel utility with the use of the NPOI package, which can
Simply takes your data table or the collection
And Returns you excel while maintaining all the data table/list data type intact in the excel.
Github Code repo.: https://github.com/ansaridawood/.NET-Generic-Excel-Export-Sample/tree/master/GenericExcelExport/ExcelExport
Looking for a code explanation, you can find it here:
https://www.codeproject.com/Articles/1241654/Export-to-Excel-using-NPOI-Csharp-and-WEB-API
It uses NPOI DLL and it has 2 cs files to include and then you are good to go
Below is the first file for reference AbstractDataExport.cs:
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
namespace GenericExcelExport.ExcelExport
{
public interface IAbstractDataExport
{
HttpResponseMessage Export(List exportData, string fileName, string sheetName);
}
public abstract class AbstractDataExport : IAbstractDataExport
{
protected string _sheetName;
protected string _fileName;
protected List _headers;
protected List _type;
protected IWorkbook _workbook;
protected ISheet _sheet;
private const string DefaultSheetName = "Sheet1";
public HttpResponseMessage Export
(List exportData, string fileName, string sheetName = DefaultSheetName)
{
_fileName = fileName;
_sheetName = sheetName;
_workbook = new XSSFWorkbook(); //Creating New Excel object
_sheet = _workbook.CreateSheet(_sheetName); //Creating New Excel Sheet object
var headerStyle = _workbook.CreateCellStyle(); //Formatting
var headerFont = _workbook.CreateFont();
headerFont.IsBold = true;
headerStyle.SetFont(headerFont);
WriteData(exportData); //your list object to NPOI excel conversion happens here
//Header
var header = _sheet.CreateRow(0);
for (var i = 0; i < _headers.Count; i++)
{
var cell = header.CreateCell(i);
cell.SetCellValue(_headers[i]);
cell.CellStyle = headerStyle;
}
for (var i = 0; i < _headers.Count; i++)
{
_sheet.AutoSizeColumn(i);
}
using (var memoryStream = new MemoryStream()) //creating memoryStream
{
_workbook.Write(memoryStream);
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(memoryStream.ToArray())
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue
("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment")
{
FileName = $"{_fileName}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx"
};
return response;
}
}
//Generic Definition to handle all types of List
public abstract void WriteData(List exportData);
}
}
and this the second and final file AbstractDataExportBridge.cs:
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text.RegularExpressions;
namespace GenericExcelExport.ExcelExport
{
public class AbstractDataExportBridge : AbstractDataExport
{
public AbstractDataExportBridge()
{
_headers = new List<string>();
_type = new List<string>();
}
public override void WriteData<T>(List<T> exportData)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
{
var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
_type.Add(type.Name);
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ??
prop.PropertyType);
string name = Regex.Replace(prop.Name, "([A-Z])", " $1").Trim(); //space separated
//name by caps for header
_headers.Add(name);
}
foreach (T item in exportData)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
IRow sheetRow = null;
for (int i = 0; i < table.Rows.Count; i++)
{
sheetRow = _sheet.CreateRow(i + 1);
for (int j = 0; j < table.Columns.Count; j++)
{
ICell Row1 = sheetRow.CreateCell(j);
string type = _type[j].ToLower();
var currentCellValue = table.Rows[i][j];
if (currentCellValue != null &&
!string.IsNullOrEmpty(Convert.ToString(currentCellValue)))
{
if (type == "string")
{
Row1.SetCellValue(Convert.ToString(currentCellValue));
}
else if (type == "int32")
{
Row1.SetCellValue(Convert.ToInt32(currentCellValue));
}
else if (type == "double")
{
Row1.SetCellValue(Convert.ToDouble(currentCellValue));
}
}
else
{
Row1.SetCellValue(string.Empty);
}
}
}
}
}
}
For a detailed explanation, refer link provided in the beginning.
I'm using NPOI to export data into excel too.
But I have a list that will pull data from another excel file that created by NPOI.
Anyway, I think my solution to solve this problem, which is not much different from yours, can be effective.
After you see the code sample below, read the description.
await using var stream = new FileStream(#"C:\Users\Sina\Desktop\TestExcel.xlsx", FileMode.OpenOrCreate, FileAccess.Write);
IWorkbook workbook = new XSSFWorkbook();
var excelSheet = workbook.CreateSheet("TestSheet");
for (var i = 0; i < MyDataList.Count(); i++)
{
var row = excelSheet.CreateRow(i);
for (var j = 0; j < MyDataList[i].Cells.Count(); j++)
{
var cell = row.CreateCell(j);
cell.SetCellValue(MyDataList[i].Cells[j].ToString());
}
}
workbook.Write(stream);
As I said, instead of the list you got the data from your database, I've used a list that has data from another excel file that I pulled through NPOI.
You can see it in the code snippet above (MyDataList).
It is of type (List<IRow>).
You have to create as many rows as there are data in your list, so create it in a loop each time. var row = excelSheet.CreateRow(i)
Now notice that each row has several cells and I fill the cells with another loop and you need to create any number of cells in your row, so create it in this loop each time. var cell = row.CreateCell(j)
You can now use cell.SetCellValue() to set each cell data then use the data in your list instead of MyDataList[i].Cells[j] in that.
Note that the input type of the SetCellValue() method must be a string.
Now I want to add that I also used the AddRange() method instead of the second loop (like this - row.Cells.AddRange(FailedRowList[i].Cells)) but it didn't work, so if you can use that I would appreciate if you say it and let me know more. I hope my answer was helpful.
Thanks
Assume I have a .csv file with 70 columns, but only 5 of the columns are what I need. I want to be able to pass a method a string array of the columns names that I want, and for it to return a datatable.
private void method(object sender, EventArgs e) {
string[] columns =
{
#"Column21",
#"Column48"
};
DataTable myDataTable = Get_DT(columns);
}
public DataTable Get_DT(string[] columns) {
DataTable ret = new DataTable();
if (columns.Length > 0)
{
foreach (string column in columns)
{
ret.Columns.Add(column);
}
string[] csvlines = File.ReadAllLines(#"path to csv file");
csvlines = csvlines.Skip(1).ToArray(); //ignore the columns in the first line of the csv file
//this is where i need help... i want to use linq to read the fields
//of the each row with only the columns name given in the string[]
//named columns
}
return ret;
}
Read the first line of the file, line.Split(',') (or whatever your delimiter is), then get the index of each column name and store that.
Then for each other line, again do a var values = line.Split(','), then get the values from the columns.
Quick and dirty version:
string[] csvlines = File.ReadAllLines(#"path to csv file");
//select the indices of the columns we want
var cols = csvlines[0].Split(',').Select((val,i) => new { val, i }).Where(x => columns.Any(c => c == x.val)).Select(x => x.i).ToList();
//now go through the remaining lines
foreach (var line in csvlines.Skip(1))
{
var line_values = line.Split(',').ToList();
var dt_values = line_values.Where(x => cols.Contains(line_values.IndexOf(x)));
//now do something with the values you got for this row, add them to your datatable
}
You can look at https://joshclose.github.io/CsvHelper/
Think Reading individual fields is what you are looking for
var csv = new CsvReader( textReader );
while( csv.Read() )
{
var intField = csv.GetField<int>( 0 );
var stringField = csv.GetField<string>( 1 );
var boolField = csv.GetField<bool>( "HeaderName" );
}
We can easily do this without writing much code.
Exceldatareader is an awesome dll for that, it will directly as a datable from the excel sheet with just one method.
here is the links for example:http://www.c-sharpcorner.com/blogs/using-iexceldatareader1
http://exceldatareader.codeplex.com/
Hope it was useful kindly let me know your thoughts or feedbacks
Thanks
Karthik
var data = File.ReadAllLines(#"path to csv file");
// the expenses row
var query = data.Single(d => d[0] == "Expenses");
//third column
int column21 = 3;
return query[column21];
As others have stated a library like CsvReader can be used for this. As for linq, I don't think its suitable for this kind of job.
I haven't tested this but it should get you through
using (TextReader textReader = new StreamReader(filePath))
{
using (var csvReader = new CsvReader(textReader))
{
var headers = csvReader.FieldHeaders;
for (int rowIndex = 0; csvReader.Read(); rowIndex++)
{
var dataRow = dataTable.NewRow();
for (int chosenColumnIndex = 0; chosenColumnIndex < columns.Count(); chosenColumnIndex++)
{
for (int headerIndex = 0; headerIndex < headers.Length; headerIndex++)
{
if (headers[headerIndex] == columns[chosenColumnIndex])
{
dataRow[chosenColumnIndex] = csvReader.GetField<string>(headerIndex);
}
}
}
dataTable.Rows.InsertAt(dataRow, rowIndex);
}
}
}
I'm trying to use Excel data reader introduced here http://fabiouechi.blogspot.fi/2010/07/excel-data-driven-tests-with-nunit.html to read data for my NUnit tests.
My test data has several columns - like status, running, pressure, p_prev, temperature - and over 200 rows in excel file.
I'm using the following code to read test cases.
public static IEnumerable<TestCaseData> TestCaseData_T3003
{
get
{
var testcases = ExcelTestCaseDataReader.New()
.FromFileSystem(#"C:\Tests\Test data.xlsx")
.AddSheet("T3003")
.GetTestCases(delegate(string sheet, DataRow row, int rowNum)
{
var testName = sheet + rowNum;
//var category = Convert.ToString(row["col1"]);
IDictionary testDataArgs = new Hashtable();
var testData = new TestCaseData(testDataArgs).SetName(testName);
return testData;
}
);
foreach (TestCaseData testCaseData in testcases)
{
yield return testCaseData;
}
}
}
public List<TestCaseData> GetTestCases(Func<string, DataRow, int, TestCaseData> testCaseDataCreator)
{
var testDataList = new List<TestCaseData>();
IExcelDataReader excelReader = GetExcelReader(ExcelFile);
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
foreach (var sheet in Sheets)
{
var sheetTable = result.Tables[sheet];
var i = 0;
foreach (DataRow dr in sheetTable.Rows)
{
testDataList.Add(testCaseDataCreator(sheet, dr, i));
i = i + 1;
}
}
excelReader.Close();
return testDataList;
}
and the actual test, which uses data from excel, is still very raw.
[Test]
[TestCaseSource("TestCaseData_T3003")]
public void T3003_Excel(IDictionary testData)
{
//do the assertions here
}
The question is, how do I access the test data in my test procedure? How do I refer to the value in a column "status" or "pressure"?
Nunit finds all rows in my test data, because it runs the test for 214 times.
But, when I debug my code and bread in T3003_Excel, the property testData.Count is zero. So is the length of the key collection of hashtable testData.Keys. (testData.Keys.Count = 0)
Any suggestions or help?
You're just adding an empty HashTable to the test case data; you need to actually put something in it. Your delegate should be something like this:
...
.GetTestCases(delegate(string sheet, DataRow row, int rowNum)
{
var testDataArgs = new Dictionary<string, object>();
foreach (DataColumn column in row.Table.Columns)
{
testDataArgs[column.ColumnName] = row[column];
}
var testName = sheet + rowNum;
return new TestCaseData(testDataArgs).SetName(testName);
}
Answer Summary:
Solved this problem using Jon Skeet's answer below. Here is the finished code
public static CSVData CreateCSVData(List<RegDataDisplay> rList,
string[] selectors)
{
CSVData csv = new CSVData(); // Create the CSVData object
foreach(string selector in selectors)
{
// Get the PropertyInfo for the property whose name
// is the value of selector
var property = typeof(RegDataDisplay).GetProperty(selector);
// Use LINQ to get a list of the values for the specified property
// of each RegDataDisplay object in the supplied list.
var values = rList.Select(row => property.GetValue(row, null)
.ToString());
// Create a new list with the property name to use as a header
List<string> templs = new List<string>(){selector};
// Add the returned values after the header
templs.AddRange(values);
// Add this list as a column for the CSVData object.
csv.Columns.Add(templs);
}
return csv;
}
Question
I am building my SQL query dynamically from user input, and then exporting the results to a CSV file. I have a class called RegDataDisplay which has a property for each of the possible columns returned by my query. I can tell what columns are being selected but in my CSV creator I need to be able to only output those specific columns.
In the example below, all of the data I have retrieved is in rList, and the names of the properties I need are in selectors. So I want to iterate through the list and then add only the properties I need to my CSV data.
public static CSVData CreateCSVData(List<RegDataDisplay> rList, string[] selectors)
{
CSVData csv = new CSVData();
for(int i = 0; i < selectors.Length; i++)
{
csv.Columns.Add(new List<string>(){selectors[i]});
}
// So now I have the headers for the CSV columns,
// I need the specific properties only which is where I'm stuck
for(int i = 0; i < selectors.Length; i++)
{
for(int j = 0; j < rList.Count; j++)
{
// If it was javascript I would do something like this
csv.Columns[i].Add(rList[j][selectors[i]]);
}
}
}
Thanks
EDIT: On the right track now but I'm coming up against an error "Object does not match target type".
public static CSVData CreateCSVData()
{
// I've created a test method with test data
string[] selectors = new string[] { "Firstname", "Lastname" };
List<RegDataDisplay> rList = new List<RegDataDisplay>();
RegDataDisplay rd = new RegDataDisplay();
rd.Firstname = "first";
rd.Lastname = "last";
rList.Add(rd);
CSVData csv = new CSVData();
foreach(string selector in selectors)
{
var property = typeof(RegDataDisplay).GetProperty(selector);
var values = rList.Select(row => property.GetValue(rList, null).ToString())
.ToList(); // Error throws here
csv.Columns.Add(values);
}
return csv;
}
Assuming you're on .NET 3.5 or higher, it sounds like you may want something like:
public static CSVData CreateCSVData(List<RegDataDisplay> rList,
string[] selectors)
{
CSVData csv = new CSVData();
foreach (string selector in selectors)
{
var prop = typeof(RegDataDisplay).GetProperty(selector);
var values = rList.Select(row => (string) prop.GetValue(row, null))
.ToList();
csv.Columns.Add(values);
}
}