Excel to DataTable using EPPlus - excel locked for editing - c#

I'm using the following code to convert an Excel to a datatable using EPPlus:
public DataTable ExcelToDataTable(string path)
{
var pck = new OfficeOpenXml.ExcelPackage();
pck.Load(File.OpenRead(path));
var ws = pck.Workbook.Worksheets.First();
DataTable tbl = new DataTable();
bool hasHeader = true;
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
}
var startRow = hasHeader ? 2 : 1;
for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
var row = tbl.NewRow();
foreach (var cell in wsRow)
{
row[cell.Start.Column - 1] = cell.Text;
}
tbl.Rows.Add(row);
}
pck.Dispose();
return tbl;
}
It creates the Excel, however, when I try to open it, it gives me the message that it is locked for editing by another user and that I can only open it in Read-Only mode.
I thought using:
pck.Dispose();
would solve the issue, however I'm still getting the same error.
Also, when I try to delete the file, I get the message: The action can't be completed because the file is open in WebDev.WebServer40.EXE.
Any ideas how to resolve this?
Thanks in advance. :)

I see, that's what i've posted recently here(now corrected). It can be improved since the ExcelPackage and the FileStream(from File.OpenRead) are not disposed after using.
public static DataTable GetDataTableFromExcel(string path, bool hasHeader = true)
{
using (var pck = new OfficeOpenXml.ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
var ws = pck.Workbook.Worksheets.First();
DataTable tbl = new DataTable();
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
}
var startRow = hasHeader ? 2 : 1;
for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
DataRow row = tbl.Rows.Add();
foreach (var cell in wsRow)
{
row[cell.Start.Column - 1] = cell.Text;
}
}
return tbl;
}
}

A extension version of Tim Schmelter's answer.
public static DataTable ToDataTable(this ExcelWorksheet ws, bool hasHeaderRow = true)
{
var tbl = new DataTable();
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
tbl.Columns.Add(hasHeaderRow ?
firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
var startRow = hasHeaderRow ? 2 : 1;
for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
var row = tbl.NewRow();
foreach (var cell in wsRow) row[cell.Start.Column - 1] = cell.Text;
tbl.Rows.Add(row);
}
return tbl;
}

I've created a method that converts an Excel file to a DataTable using EPPlus, and tried to maintain Type Safety. Also duplicate column names are handled and with a boolean you can tell the method wether the sheet has a row with headers. I've created it for a complex import process that has several steps after uploading that requires user input before committing to the database.
private DataTable ExcelToDataTable(byte[] excelDocumentAsBytes, bool hasHeaderRow)
{
DataTable dt = new DataTable();
string errorMessages = "";
//create a new Excel package in a memorystream
using (MemoryStream stream = new MemoryStream(excelDocumentAsBytes))
using (ExcelPackage excelPackage = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
//check if the worksheet is completely empty
if (worksheet.Dimension == null)
{
return dt;
}
//add the columns to the datatable
for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column; j++)
{
string columnName = "Column " + j;
var excelCell = worksheet.Cells[1, j].Value;
if (excelCell != null)
{
var excelCellDataType = excelCell;
//if there is a headerrow, set the next cell for the datatype and set the column name
if (hasHeaderRow == true)
{
excelCellDataType = worksheet.Cells[2, j].Value;
columnName = excelCell.ToString();
//check if the column name already exists in the datatable, if so make a unique name
if (dt.Columns.Contains(columnName) == true)
{
columnName = columnName + "_" + j;
}
}
//try to determine the datatype for the column (by looking at the next column if there is a header row)
if (excelCellDataType is DateTime)
{
dt.Columns.Add(columnName, typeof(DateTime));
}
else if (excelCellDataType is Boolean)
{
dt.Columns.Add(columnName, typeof(Boolean));
}
else if (excelCellDataType is Double)
{
//determine if the value is a decimal or int by looking for a decimal separator
//not the cleanest of solutions but it works since excel always gives a double
if (excelCellDataType.ToString().Contains(".") || excelCellDataType.ToString().Contains(","))
{
dt.Columns.Add(columnName, typeof(Decimal));
}
else
{
dt.Columns.Add(columnName, typeof(Int64));
}
}
else
{
dt.Columns.Add(columnName, typeof(String));
}
}
else
{
dt.Columns.Add(columnName, typeof(String));
}
}
//start adding data the datatable here by looping all rows and columns
for (int i = worksheet.Dimension.Start.Row + Convert.ToInt32(hasHeaderRow); i <= worksheet.Dimension.End.Row; i++)
{
//create a new datatable row
DataRow row = dt.NewRow();
//loop all columns
for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column; j++)
{
var excelCell = worksheet.Cells[i, j].Value;
//add cell value to the datatable
if (excelCell != null)
{
try
{
row[j - 1] = excelCell;
}
catch
{
errorMessages += "Row " + (i - 1) + ", Column " + j + ". Invalid " + dt.Columns[j - 1].DataType.ToString().Replace("System.", "") + " value: " + excelCell.ToString() + "<br>";
}
}
}
//add the new row to the datatable
dt.Rows.Add(row);
}
}
//show error messages if needed
Label1.Text = errorMessages;
return dt;
}
The webforms button click for demo purposes.
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
DataTable dt = ExcelToDataTable(FileUpload1.FileBytes, CheckBox1.Checked);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}

VDWWD's answer above works great to keep type safety, and I built upon it with some improvements.
Method reads from a file directly.
Column type detection by using all rows and not just one value. Column type is set to String if more than type is found in the column.
Error Messages returned in a list of strings.
Here is the updated version:
public static DataTable ExcelToDataTable(string path, ref List<string> errorList, bool hasHeaderRow = true )
{
DataTable dt = new DataTable();
errorList = new List<string>();
//create a new Excel package
using (ExcelPackage excelPackage = new ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
excelPackage.Load(stream);
}
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
//check if the worksheet is completely empty
if (worksheet.Dimension == null)
{
return dt;
}
//add the columns to the datatable
for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column; j++)
{
string columnName = "Column " + j;
//Build hashset with all types in the row
var columnTypes = new HashSet<Type>();
for (int i = worksheet.Dimension.Start.Row + Convert.ToInt32(hasHeaderRow); i <= worksheet.Dimension.End.Row; i++)
{
//Only add type if cell value not empty
if (worksheet.Cells[i, j].Value != null)
{
columnTypes.Add(worksheet.Cells[i, j].Value.GetType());
}
}
var excelCell = worksheet.Cells[1, j].Value;
if (excelCell != null)
{
Type excelCellDataType = null;
//if there is a headerrow, set the next cell for the datatype and set the column name
if (hasHeaderRow == true)
{
columnName = excelCell.ToString();
//check if the column name already exists in the datatable, if so make a unique name
if (dt.Columns.Contains(columnName) == true)
{
columnName = columnName + "_" + j;
}
}
//Select input type for the column
if (columnTypes.Count == 1)
{
excelCellDataType = columnTypes.First();
}
else
{
excelCellDataType = typeof(string);
}
//try to determine the datatype for the column (by looking at the next column if there is a header row)
if (excelCellDataType == typeof(DateTime))
{
dt.Columns.Add(columnName, typeof(DateTime));
}
else if (excelCellDataType == typeof(Boolean))
{
dt.Columns.Add(columnName, typeof(Boolean));
}
else if (excelCellDataType == typeof(Double))
{
//determine if the value is a decimal or int by looking for a decimal separator
//not the cleanest of solutions but it works since excel always gives a double
if (excelCellDataType.ToString().Contains(".") || excelCellDataType.ToString().Contains(","))
{
dt.Columns.Add(columnName, typeof(Decimal));
}
else
{
dt.Columns.Add(columnName, typeof(Int64));
}
}
else
{
dt.Columns.Add(columnName, typeof(String));
}
}
else
{
dt.Columns.Add(columnName, typeof(String));
}
}
//start adding data the datatable here by looping all rows and columns
for (int i = worksheet.Dimension.Start.Row + Convert.ToInt32(hasHeaderRow); i <= worksheet.Dimension.End.Row; i++)
{
//create a new datatable row
DataRow row = dt.NewRow();
//loop all columns
for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column; j++)
{
var excelCell = worksheet.Cells[i, j].Value;
//add cell value to the datatable
if (excelCell != null)
{
try
{
row[j - 1] = excelCell;
}
catch
{
errorList.Add("Row " + (i - 1) + ", Column " + j + ". Invalid " + dt.Columns[j - 1].DataType.ToString().Replace("System.", "") + " value: " + excelCell.ToString() );
}
}
}
//add the new row to the datatable
dt.Rows.Add(row);
}
}
return dt;
}

This is an improvement to the generic one above. Use is if you have a class with the following properties, "Name", "Surname", "Telephone", "Fax" and you have a excel sheet with the first row with the same names, it will load the excel rows into a class object and pop it into a List
public static List<T> GetClassFromExcel<T>(string path, int fromRow, int fromColumn, int toRow = 0, int toColumn = 0)
{
if (toColumn != 0 && toColumn < fromColumn) throw new Exception("toColumn can not be less than fromColumn");
if (toRow != 0 && toRow < fromRow) throw new Exception("toRow can not be less than fromRow");
List<T> retList = new List<T>();
using (var pck = new ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
//Retrieve first Worksheet
var ws = pck.Workbook.Worksheets.First();
//If the to column is empty or 0, then make the tocolumn to the count of the properties
//Of the class object inserted
toColumn = toColumn == 0 ? typeof(T).GetProperties().Count() : toColumn;
//Read the first Row for the column names and place into a list so that
//it can be used as reference to properties
Dictionary<string, int> columnNames = new Dictionary<string, int>();
// wsRow = ws.Row(0);
var colPosition = 0;
foreach (var cell in ws.Cells[1, 1, 1, toColumn == 0 ? ws.Dimension.Columns : toColumn])
{
columnNames.Add(cell.Value.ToString(), colPosition);
colPosition++;
}
//create a instance of T
T objT = Activator.CreateInstance<T>();
//Retrieve the type of T
Type myType = typeof(T);
//Get all the properties associated with T
PropertyInfo[] myProp = myType.GetProperties();
//Loop through the rows of the excel sheet
for (var rowNum = fromRow; rowNum <= (toRow == 0? ws.Dimension.End.Row : toRow); rowNum++)
{
var wsRow = ws.Cells[rowNum, fromColumn, rowNum, ws.Cells.Count()];
foreach (var propertyInfo in myProp)
{
if (columnNames.ContainsKey(propertyInfo.Name))
{
int position = 0;
columnNames.TryGetValue(propertyInfo.Name, out position);
//int position = columnNames.IndexOf(propertyInfo.Name);
//To prevent an exception cast the value to the type of the property.
propertyInfo.SetValue(objT, Convert.ChangeType(wsRow[rowNum, position + 1].Value, propertyInfo.PropertyType));
}
}
retList.Add(objT);
}
}
return retList;
}
now you can use the list as a databinding source if you need...
A give from me to you... :) Daniel C. Vrey
Updated it for toColumn to work and added toRow and followed Andreas suggestions. Thumbs up for Andreas

public static List<T> getClassFromExcel<T>(string path, int fromRow, int fromColumn, int toColumn = 0) where T : class
{
using (var pck = new OfficeOpenXml.ExcelPackage())
{
List<T> retList = new List<T>();
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
var ws = pck.Workbook.Worksheets.First();
toColumn = toColumn == 0 ? typeof(T).GetProperties().Count() : toColumn;
for (var rowNum = fromRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
T objT = Activator.CreateInstance<T>();
Type myType = typeof(T);
PropertyInfo[] myProp = myType.GetProperties();
var wsRow = ws.Cells[rowNum, fromColumn, rowNum, toColumn];
for (int i = 0; i < myProp.Count(); i++)
{
myProp[i].SetValue(objT, wsRow[rowNum, fromColumn + i].Text);
}
retList.Add(objT);
}
return retList;
}
}

public static List<T> GetClassFromExcel<T>(string path, int fromRow, int fromColumn, int toRow = 0, int toColumn = 0) where T: class, new()
{
if (toColumn != 0 && toColumn < fromColumn) throw new Exception("toColumn can not be less than fromColumn");
if (toRow != 0 && toRow < fromRow) throw new Exception("toRow can not be less than fromRow");
List<T> retList = new List<T>();
using (var pck = new ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
//Retrieve first Worksheet
var ws = pck.Workbook.Worksheets.First();
toColumn = toColumn == 0 ? typeof(T).GetProperties().Count() : toColumn; //If the to column is empty or 0, then make the tocolumn to the count of the properties Of the class object inserted
//Read the first Row for the column names and place into a list so that
//it can be used as reference to properties
Dictionary<string, int> columnNames = new Dictionary<string, int>();
// wsRow = ws.Row(0);
var colPosition = 0;
foreach (var cell in ws.Cells[1, 1, 1, toColumn == 0 ? ws.Dimension.Columns : toColumn])
{
columnNames.Add(cell.Value.ToString(), colPosition);
colPosition++;
}
//Retrieve the type of T
Type myType = typeof(T);
//Get all the properties associated with T
PropertyInfo[] myProp = myType.GetProperties();
//Loop through the rows of the excel sheet
for (var rowNum = fromRow + 1; rowNum <= (toRow == 0 ? ws.Dimension.End.Row : toRow); rowNum++) // fromRow + 1 to read from next row after columnheader
{
//create a instance of T
//T objT = Activator.CreateInstance<T>();
T objT = new T();
// var wsRow = ws.Cells[rowNum, fromColumn, rowNum, ws.Cells.Count()]; //ws.Cells.Count() causing out of range error hence using ws.Dimension.Columns to get last column index
var wsRow = ws.Cells[rowNum, fromColumn, rowNum, ws.Dimension.Columns];
foreach (var propertyInfo in myProp)
{
var attribute = propertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true).Cast<DisplayNameAttribute>().SingleOrDefault();
string displayName = attribute != null && !string.IsNullOrEmpty(attribute.DisplayName) ? attribute.DisplayName : propertyInfo.Name; // If DisplayName annotation not used then get property name itself
if (columnNames.ContainsKey(displayName))
{
int position = 0;
columnNames.TryGetValue(displayName, out position);
////int position = columnNames.IndexOf(propertyInfo.Name);
////To prevent an exception cast the value to the type of the property.
propertyInfo.SetValue(objT, Convert.ChangeType(wsRow[rowNum, position + 1].Value, propertyInfo.PropertyType));
}
}
retList.Add(objT);
}
}
return retList;
}
//IMPLEMENTATION DONE BY PLACING Code IT IN SEPARATE Helpers.CS file and
//Consuming it in this manner
List<CustomerExcelModel> records =
Helpers.GetClassFromExcel<CustomerExcelModel>(filelocation, 1, 1);
Thanks a lot to the user who Submitted code and Andreas for suggestion
Here are the Following changes done, i am new to generics so forgive and correct me for any mistakes please find modified code below it might help someone
Added Display Annotation entity model to map with the Excel Column
name so that Column Name with spaces can also be handled.
had issue "T objT " as it was outside of for loop and hence caused
same value repeatedly inserted into List fixed it by
instantiating inside loop i.e using "new T()"
Fixed Column out of range error by using "ws.Dimension.Columns" to get Column count , instead of ws.Cells.Count() as it caused out
range column error
for looping through row data added +1 to it ,as RowNum=1 was reading header name also so done minor change of "rowNum = fromRow + 1"

Here I am sharing how you can read the excel. You can modify it to store each date in datatables.
public void readXLS(string FilePath)
{
FileInfo existingFile = new FileInfo(FilePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
//get the first worksheet in the workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int colCount = worksheet.Dimension.End.Column; //get Column Count
int rowCount = worksheet.Dimension.End.Row; //get row count
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= colCount; col++)
{
//You can update code here to add each cell value to DataTable.
Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
}
}
}
}
Reff: http://sforsuresh.in/read-data-excel-sheet-insert-database-table-c/

Use below code if you want to read data of each worksheet from excel, as well if worksheet contain date format data for particular column.
public static DataSet ReadExcelFileToDataSet2(string filePath, bool isFirstRowHeader=true)
{
DataSet result = new DataSet();
Excel.ExcelPackage xlsPackage = new Excel.ExcelPackage(new FileInfo(filePath)); //using Excel = OfficeOpenXml; <--EPPLUS
Excel.ExcelWorkbook workBook = xlsPackage.Workbook;
try
{
for (int count = 1; count <= workBook.Worksheets.Count; count++)
{
Excel.ExcelWorksheet wsworkSheet = workBook.Worksheets[count];
if (wsworkSheet.Name.ToLower() == "sheetName")
{
wsworkSheet.Column(4).Style.Numberformat.Format = "MM-dd-yyyy"; // set column value to read as Date Type or numberformat
}
DataTable tbl = new DataTable();
// wsworkSheet.Dimension - (It will return cell dimesion like A1:N7 , means returning the worksheet dimesions.)
// wsworkSheet.Dimension.End.Address - (It will return right bottom cell like N7)
// wsworkSheet.Dimension.End.Columns - (It will return count from A1 to N7 like here 14)
foreach (var firstRowCell in wsworkSheet.Cells[1, 1, 1, wsworkSheet.Dimension.End.Column]) //.Cells[Row start, Column Start, Row end, Column End]
{
var colName = "";
colName = firstRowCell.Text;
tbl.Columns.Add(isFirstRowHeader ? colName : string.Format("Column {0}", firstRowCell.Start.Column)); //Geth the Column index (index starting with 1) from the left top.
}
var startRow = isFirstRowHeader ? 2 : 1;
for (int rowNum = startRow; rowNum <= wsworkSheet.Dimension.End.Row; rowNum++)
{
var wsRow = wsworkSheet.Cells[rowNum, 1, rowNum, wsworkSheet.Dimension.End.Column]; // wsworkSheet.Cells[Row start, Column Start, Row end, Column End]
DataRow row = tbl.Rows.Add();
foreach (var cell in wsRow)
{
row[cell.Start.Column - 1] = cell.Text;
}
}
tbl.TableName = wsworkSheet.Name;
result.Tables.Add(tbl);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return result;
}

Related

NPOI number format

I'm using NPOI library to create excel file, I have problem with formatting price.
ISheet excelSheet = workbook.CreateSheet(sheetName);
ICellStyle codeCellStyle = workbook.CreateCellStyle();
ICellStyle priceCellStyle = workbook.CreateCellStyle();
ICellStyle availabilityStyle = workbook.CreateCellStyle();
excelSheet.SetColumnWidth(0, 10 * 256);
excelSheet.SetColumnWidth(1, 12 * 256);
excelSheet.SetColumnWidth(2, 15 * 256);
List<string> columns = new List<string>() { "Code", "Price", "Availability" };
IRow row = excelSheet.CreateRow(0);
foreach (var columnData in columns.Select((v, i) => new { Column = v, Index = i }).ToList())
{
row.CreateCell(columnData.Index).SetCellValue(columnData.Column);
}
codeCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("#");
priceCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("#,##0.00");
availabilityStyle.DataFormat = workbook.CreateDataFormat().GetFormat("#");
int rowIndex = 1;
foreach (var item in items)
{
row = excelSheet.CreateRow(rowIndex);
row.CreateCell(0).SetCellValue(item.Code);
row.Cells[0].CellStyle = codeCellStyle;
if (item.HasData)
{
if (item.Price == "ON DEMAND")
{
row.CreateCell(1).SetCellValue(item.Price);
}
else
{
ICell priceCell = row.CreateCell(1);
row.Cells[1].SetCellType(CellType.Numeric);
row.Cells[1].CellStyle = priceCellStyle;
if (percentage != 0)
priceCell.SetCellValue(double.Parse(item.Price) + ((double)percentage / (double)100) * double.Parse(item.Price));
else
priceCell.SetCellValue(double.Parse(item.Price));
}
row.CreateCell(2).SetCellValue(item.Availability ? "True" : "False");
}
else
{
row.CreateCell(1).SetCellValue("");
row.CreateCell(2).SetCellValue("");
}
rowIndex++;
}
When I have item with price "422,26" it produce excel file with price value 42226,00.
It works fine when I have price "380,00", "4730,00", but problem occurs when I have decimal part in string number.
I tried other suggestions from stackoverflow but I have no luck nothing works on my example.
After struggling this format solved my problem
"#,#0.00"

Copy multiple selected rows from one grid to another

I have this code that copy selected rows from one grid to another
private void btnAddEmployee_Click(object sender, EventArgs e)
{
LayoutControl lc = new LayoutControl();
lc.Dock = DockStyle.Top;
LookUpEdit userShift = new LookUpEdit();
userShift.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
userShift.Properties.DataSource = paint.GetShiftTime();
userShift.Properties.DisplayMember = "ShiftTime";
userShift.Properties.ValueMember = "id";
userShift.Properties.ShowHeader = false;
var date = DateTime.Now;
if (8 < date.Hour && date.Hour < 16)
{
userShift.EditValue = 1;
}
else if (16 < date.Hour && date.Hour < 24)
{
userShift.EditValue = 2;
}
else
{
userShift.EditValue = 3;
}
lc.AddItem(Resources.workingHours, userShift).TextVisible = true;
lc.Height = 50;
this.Controls.Add(lc);
this.Dock = DockStyle.Top;
int[] selectedRows = gridView4.GetSelectedRows();
for(int n=0;n< selectedRows.Length;n++)
//foreach (int index in selectedRows)
{
if (DevExpress.XtraEditors.XtraDialog.Show(lc, Resources.options, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
//Prevent duplicate data
for (int i = 0; i < gridView5.RowCount; i++)
{
if (gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Matricule").ToString() == gridView5.GetRowCellValue(i, "Matricule").ToString())
{
XtraMessageBox.Show(Resources.employeeAlreadyAdded, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
DataRow r = EmplDT.NewRow();
r[0] = gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Matricule").ToString();
r[1] = gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Employé").ToString();
r[2] = userShift.Text;
r[3] = userShift.EditValue;
r[4] = txtDate.EditValue;
EmplDT.Rows.Add(r);
this is my code to create Columns in gridview 5
DataTable EmplDT = new DataTable();
void CreateEmployeeTable()
{
EmplDT.Columns.Add("Matricule");
EmplDT.Columns.Add("Employé");
EmplDT.Columns.Add("Heure");
EmplDT.Columns.Add("idShiftTime", typeof(Int32));
EmplDT.Columns.Add("Date", typeof(DateTime));
gridControl5.DataSource = EmplDT;
gridView5.Columns["idShiftTime"].Visible = false;
gridView5.Columns["Date"].Visible = false;
}
i have two problems in this code:
the first one is when i run the code it only add the first record and then i get error message of duplicate .
the second one i want to show layout control only the first time.
thanks in advance and sorry for my english.
Looping thru selected rows from a devexpress gridview and getting a row can be much simpler like this
int[] selectedRows = gridView4.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
// Get a DataRow and fill it with all values from the this selected row
// This is where you went wrong, you kept using only the first selected row
DataRow rowGridView4 = (gridView4.GetRow(selectedRows[i]) as DataRowView).Row;
// Do a check for doubles here
DataRow[] doubles = EmplDT.Select("Matricule = '" + rowGridView4[0].ToString() +"'");
if (doubles.Length > 0)
{
XtraMessageBox.Show(Resources.employeeAlreadyAdded, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// fix for error "This row already belongs to another table"
DataRox row = EmplDT.NewRow();
row[0] = rowGridView4[0];
row[1] = rowGridView4[1];
row[2] = userShift.Text;
row[3] = userShift.EditValue;
row[4] = txtDate.EditValue;
EmplDT.Rows.Add(row);
}
Please note that with testing for doubles at this place will cause all records to be copied until a duplicate is found. So after your error message there might be some records copied and some not.
Is that how you intended this ?
I would leave out the error message and just skip duplicate records. You can still show a message with howmany records where copied if you want.
int[] selectedRows = gridView4.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
// Get a DataRow and fill it with all values from the this selected row
// This is where you went wrong, you kept using only the first selected row
DataRow rowGridView4 = (gridView4.GetRow(selectedRows[i]) as DataRowView).Row;
// Do a check for doubles here
DataRow[] doubles = EmplDT.Select("Matricule = '" + rowGridView4[0].ToString() + "'");
if (doubles.Length == 0)
{
// fix for error "This row already belongs to another table"
DataRox row = EmplDT.NewRow();
row[0] = rowGridView4[0];
row[1] = rowGridView4[1];
row[2] = userShift.Text;
row[3] = userShift.EditValue;
row[4] = txtDate.EditValue;
EmplDT.Rows.Add(row);
}
}

How to read data fast from an excel and convert it to list from file stream

I am using EPPlus.
The excel I am uploading has column headers in row number 2 . And from row 4 onward it has the data which may vary up to 2k records.
The way I am doing it , it takes a lot of time for reading 2k records and putting to a list .
using (var excel = new ExcelPackage(hpf.InputStream))
{
var ws = excel.Workbook.Worksheets["Sheet1"];
//Read the file into memory
for (int rw = 4; rw <= ws.Dimension.End.Row; rw++)
{
if (!ws.Cells[rw, 1, rw, 24].All(c => c.Value == null))
{
int headerRow = 2;
GroupMembershipUploadInput gm = new GroupMembershipUploadInput();
for (int col = ws.Dimension.Start.Column; col <= ws.Dimension.End.Column; col++)
{
var s = ws.Cells[rw, col].Value;
if (ws.Cells[headerRow, col].Value.ToString().Equals("Existing Constituent Master Id"))
{
gm.cnst_mstr_id = (ws.Cells[rw, col].Value ?? (Object)"").ToString();
}
else if (ws.Cells[headerRow, col].Value.ToString().Equals("Prefix of the constituent(Mr, Mrs etc)"))
{
gm.cnst_prefix_nm = (ws.Cells[rw, col].Value ?? (Object)"").ToString();
}
}
lgl.GroupMembershipUploadInputList.Add(gm);
}
}
GroupMembershipUploadInputList is the list of objects of type GroupMembershipUploadInput that I am adding the excel values to after reading from the cell wise.
Can it be done faster ? What am I missing here ?
Please help to improve the performance.
You are making a lot iterations there, for each row, you visit each column twice. I assume that you only need those two values per row and if so the following code would reduce time drastically:
using (var excel = new ExcelPackage(hpf.InputStream))
{
var ws = excel.Workbook.Worksheets["Sheet1"];
int headerRow = 2;
// hold the colum index based on the value in the header
int col_cnst_mstr_id = 2;
int col_cnst_prefix_nm = 4;
// loop once over the columns to fetch the column index
for (int col = ws.Dimension.Start.Column; col <= ws.Dimension.End.Column; col++)
{
if ("Existing Constituent Master Id".Equals(ws.Cells[headerRow, col].Value))
{
col_cnst_mstr_id = col;
}
if ("Prefix of the constituent(Mr, Mrs etc)".Equals(ws.Cells[headerRow, col].Value))
{
col_cnst_prefix_nm = col;
}
}
//Read the file into memory
// loop over all rows
for (int rw = 4; rw <= ws.Dimension.End.Row; rw++)
{
// check if both values are not null
if (ws.Cells[rw, col_cnst_mstr_id].Value != null &&
ws.Cells[rw, col_cnst_prefix_nm].Value != null)
{
// the correct cell will be selcted based on the column index
var gm = new GroupMembershipUploadInput
{
cnst_mstr_id = (string) ws.Cells[rw, col_cnst_mstr_id].Value ?? String.Empty,
cnst_prefix_nm = (string) ws.Cells[rw, col_cnst_prefix_nm].Value ?? String.Empty
};
lgl.GroupMembershipUploadInputList.Add(gm);
}
}
}
I removed the inner column loop and moved it to the start of the method. There it is used to just get the columnindex for each field you're interested in. The expensive null check can now also be reduced. To fetch the value, all that is now needed is a simple index lookup in the row.

C# Is there a way to get table object in excel worksheet

I'm looking for a way to retrieve some kind of table object which was created in Excel sheet and then operate on it.
I tried something like this:
Excel.Worksheet activeWorksheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
foreach (var sheetObject in activeWorksheet.ListObjects)
{
if ((sheetObject as Excel.TableObject) != null)
{
var table = (Excel.TableObject)sheetObject;
}
if ((sheetObject as Excel.DataTable) != null)
{
var table = (Excel.DataTable)sheetObject;
}
if ((sheetObject as Excel.PivotTable) != null)
{
var table = (Excel.PivotTable)sheetObject;
}
if ((sheetObject as Excel.ModelTable) != null)
{
var table = (Excel.ModelTable)sheetObject;
}
if ((sheetObject as Excel.QueryTables) != null)
{
var table = (Excel.QueryTables)sheetObject;
}
}
Unfortunately any of this cast is returning anything else then null.
Do you know maybe some other way to retrieve such table in C#?
To work just on tables created in worksheet, I've used 'Range' as suggested in previous answer:
Excel.Worksheet activeWorksheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
foreach (Excel.ListObject table in activeWorksheet.ListObjects)
{
Excel.Range tableRange = table.Range;
String[] dataInRows = new string[tableRange.Rows.Count];
int i = 0;
foreach (Excel.Range row in tableRange.Rows)
{
for (int j = 0; j < row.Columns.Count; j++)
{
if (row.Cells[1, j + 1].Value2 != null)
{
dataInRows[i] = dataInRows[i] + "_" + row.Cells[1, j + 1].Value2.ToString();
}
}
i++;
}
}
Maybe Range?
from: http://www.dotnetperls.com/excel
Range excelRange = sheet.UsedRange;
object[,] valueArray = (object[,])excelRange.get_Value(
XlRangeValueDataType.xlRangeValueDefault);
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range_members(v=vs.15).aspx

Excel file with openxml with multiple sheets in single workbook

I am trying to insert data to multiple sheets. For my excel, I have two sheets which are "charts" and "ChartData". I'm able to update the data in sheet2, chartdata sheet, but I'm unable to insert data to sheet1. Here is code which I have tried to insert data into excel sheets. Here data is coming from database.
double ticks = DateTime.Now.Ticks;
// MarketAnalysis ms = new MarketAnalysis();
//ms.Marketanalysis();
File.Copy(Srcpath, #"E:\Works\OpenXML\DownloadTemplates\ExcelGenerated" + ticks + ".xlsx", true);
using (SpreadsheetDocument myworkbok = SpreadsheetDocument.Open(#"E:\Works\OpenXML\DownloadTemplates\ExcelGenerated" + ticks + ".xlsx", true))
{
//Acess the main workbook which contain all the references
WorkbookPart workbookpart = myworkbok.WorkbookPart;
//Get sheet by name
Sheet sheet = workbookpart.Workbook.Descendants<Sheet>().Where(s => s.Name == "ChartData").FirstOrDefault();
//Worksheet Part by ID
WorksheetPart worksheetpart = workbookpart.GetPartById(sheet.Id) as WorksheetPart;
//Sheet data contains all the data
SheetData sheetdata = worksheetpart.Worksheet.GetFirstChild<SheetData>();
DataSet ds = db.Chart1Data();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
// if (ds.Tables[0].Rows !=DBNull)
//{
string Rowlabel = ds.Tables[0].Rows[i][0].ToString();
// float? FY13Actuval = Convert.ToInt32(ds.Tables[0].Rows[i][1]);
if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i][1].ToString()))
{
// string s = ds.Tables[0].Rows[i][1].ToString();
//FY13Actuval=float.Parse(ds.Tables[0].Rows[i][1].ToString());
FY13Actuval = Convert.ToDouble(ds.Tables[0].Rows[i][1].ToString());
}
else
{
FY13Actuval = null;
}
double? Budget = Convert.ToDouble(ds.Tables[0].Rows[i][2].ToString());
double? Actuval = Convert.ToDouble(ds.Tables[0].Rows[i][3].ToString());
Row contentrow = CreateContentRow(index, Product, Actual, Budget, Forecast);
index++;
sheetdata.AppendChild(contentrow);
// }
}
.......Same code for the other 3 charts
Sheet sheet1 = workbookpart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Charts").FirstOrDefault();
WorksheetPart worksheetpart1 = workbookpart.GetPartById(sheet1.Id) as WorksheetPart;
//Sheet data contains all the data
SheetData sheetdata1 = worksheetpart1.Worksheet.GetFirstChild<SheetData>();
DataSet dsTbl = db.Table();
int SCMTblIndex=5;
for (int i = 0; i < dsTbl.Tables[0].Rows.Count; i++)
{
Row tblRow = CreateScorecardMetricTblRow(SCMTblIndex, dsTbl.Tables[0].Rows[i][0].ToString(),
dsTbl.Tables[0].Rows[i][1].ToString(),
dsTbl.Tables[0].Rows[i][2].ToString());
SCMTblIndex++;
sheetdata1.AppendChild(tblRow);
}
myworkbok.WorkbookPart.Workbook.Save();
}
and the Methods to create the cells:
public static string[] headerColumns = new string[]{ "A", "B", "C", "D", "E", "F", "G", "I", "J" };
public static string[] header = new string[] { "D", "E", "F" };
private static Row CreateContentRow(int index, string Product, double? Actual, double? Budget, double? Forecast)
{
//Create New ROw
Row r = new Row();
r.RowIndex = (UInt32)index;
//Begin colums
Cell c0 = new Cell();
c0.CellReference = headerColumns[0] + index;
CellValue v0 = new CellValue();
v0.Text = Product;
c0.AppendChild(v0);
r.AppendChild(c0);
Cell c1 = new Cell();
c1.CellReference = headerColumns[1] + index;
CellValue v1 = new CellValue();
v1.Text = Actual.ToString();
c1.AppendChild(v1);
r.AppendChild(c1);
Cell c2 = new Cell();
c2.CellReference = headerColumns[2] + index;
CellValue v2 = new CellValue();
v2.Text = Budget.ToString();
c2.AppendChild(v2);
r.AppendChild(c2);
Cell c3 = new Cell();
c3.CellReference = headerColumns[3] + index;
CellValue v3 = new CellValue();
v3.Text = Forecast.ToString();
c3.AppendChild(v3);
r.AppendChild(c3);
return r;
}
public static Row CreateScorecardMetricTblRow(int index, string Act_Data, string Bud_Data, string VarPr_Data)
{
Row r = new Row();
r.RowIndex = (UInt32)index;
//Begin Colums
Cell c0 = new Cell();
c0.CellReference = header[0] + index;
CellValue v0 = new CellValue();
v0.Text = Act_Data;
c0.AppendChild(v0);
r.AppendChild(c0);
Cell c1 = new Cell();
c1.CellReference = header[1] + index;
CellValue v1 = new CellValue();
v1.Text = Bud_Data;
c1.AppendChild(v1);
r.AppendChild(c1);
Cell c2 = new Cell();
c2.CellReference = header[2] + index;
CellValue v2 = new CellValue();
v2.Text = VarPr_Data;
c2.AppendChild(v2);
r.AppendChild(c2);
return r;
}
for multiple sheets to updated just
// Open the document for editing.
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
{
// Insert code here.
}
create a two classes for two sheets which you need to update sheet cell values.For each class constructor pass the SpreadsheetDocument object and implement insert text into a cell in a spreadsheet document.Just avoid the code for insertion of new sheet and try the insertion of the cell code only.
You can try this method.....it may be helpful..
private static Cell InsertCellInWorksheet(string columnName, int rowIndex, WorksheetPart worksheetPart)
{
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>();
string cellReference = columnName + rowIndex;
Alignment alignment1 = new Alignment() { WrapText = true };
// If the worksheet does not contain a row with the specified row index, insert one.
Row row;
row = new Row() { RowIndex = 3, StyleIndex = 1 };
if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
{
row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
}
else
{
row = new Row() { RowIndex = Convert.ToUInt32(rowIndex) };
sheetData.Append(row);
}
// If there is not a cell with the specified column name, insert one.
if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
{
Row row2;
row2 = new Row() { RowIndex = 3, StyleIndex = 1 };
Cell rCell = null;
foreach (Cell celld in row.Elements<Cell>())
{
if (string.Compare(celld.CellReference.Value, "A3", true) > 0)
{
rCell = celld;
break;
}
}
// Add the cell to the cell table at A1.
Cell newCell = new Cell() { CellReference = "C3" };
//Cell newCell1 = new Cell() { CellReference = "D3" };
row.InsertBefore(newCell, rCell);
//row.InsertBefore(newCell1, rCell);
// Set the cell value to be a numeric value of 100.
newCell.CellValue = new CellValue("#GUIDeXactLCMS#");
//newCell1.CellValue = new CellValue("EN");
newCell.DataType = new EnumValue<CellValues>(CellValues.Number);
return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
}
else
{
// Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
Cell refCell = null;
foreach (Cell cell in row.Elements<Cell>())
{
if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
{
//string val = cell.CellReference.Value;
refCell = cell;
break;
}
}
Cell newCell = new Cell() { CellReference = cellReference };
row.InsertBefore(newCell, refCell);
return newCell;
}
worksheet.Save();
}

Categories

Resources