Sunday 17 February 2013

Open XMl :Get the value from the excel depending on some row value

Using Open Xml, I have extracted the data from the excel sheet depending on a value, wherever there exist the value "CLOSED" in the "Status" column, I need its whole row and display in the grid. For example lets my excel looks like this: 


The column "Current Status" is in G column which contains some values, I need to extact the values from the excel where current status is closed and display. The code is as follows, where I have used open Xml :

the page designed with one fileuploader, ane button and a gridview to display the result. the code goes like this:

protected void Button1_Click(object sender, EventArgs e)
{

DataTable dt= ReadXml();GridView1.DataSource = dt;
GridView1.DataBind();
}


private DataTable ReadXml(){

string val = null;
DataRow rw = null;
int j = 0;
DataTable dt = TableStruc();
//get the path of the excel sheetusing (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(FileUpload1.PostedFile.FileName, false)){

//open the sheetvar sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants<Sheet>().First();
WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(sheets.Id);
Worksheet worksheet = worksheetPart.Worksheet;
//Get the first sheet of the excelSheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
SharedStringTablePart stringTable = spreadsheetDocument.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
//get the total rows in the sheet to loop over the sheet to check the dataint rowCount = worksheet.Descendants<Cell>().Count<Cell>();
uint rowind = 2;
for (int i = 0; i < rowCount; i++){

//get the value of cell G where the status of CMR is being written(Closed,Open,worked...)Cell cell = GetCell(worksheet, "G", rowind);
if (cell != null){

//Get the text value of that cell in a variableval = cell.CellValue.Text;
//check the datatype of the value,if it is sting---if (val != null && cell.DataType.Value == CellValues.SharedString){

var tableEntry = stringTable.SharedStringTable.ElementAt(int.Parse(val));
if (tableEntry != null){
val = tableEntry.InnerText;

//Do Something if the status is CLosedif (val =="CLOSED"){

//a new row is added to the datatable to add the values of rw = dt.NewRow();
Row r= GetRow(worksheet, rowind);
foreach (Cell c in r.Elements<Cell>()){
val = c.CellValue.Text;

if (c.DataType!=null){

if(c.DataType.Value== CellValues.SharedString)tableEntry = stringTable.SharedStringTable.ElementAt(
int.Parse(val));val = tableEntry.InnerText;
}

elseval = c.CellValue.Text;
rw[j++]= val;
}
dt.Rows.Add(rw);
j = 0;
}

}
}
rowind++;
}
}

}
return dt;}



private static Cell GetCell(Worksheet worksheet,string columnName, uint rowIndex){

Row row = GetRow(worksheet, rowIndex);
if (row == null)
return null;
return row.Elements<Cell>().Where(c => string.Compare(c.CellReference.Value, columnName +
rowIndex,
true) == 0).First();}



private static Row GetRow(Worksheet worksheet, uint rowIndex){

try{
return worksheet.GetFirstChild<SheetData>().Elements<
Row>().Where(r => r.RowIndex == rowIndex).First();}

catch (Exception){

return null;}
}


private DataTable TableStruc(){

DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn() { ColumnName = "requestno", DataType = System.Type.GetType("System.String") };dt.Columns.Add(dc1);

DataColumn dc2 = new DataColumn() { ColumnName = "group", DataType = System.Type.GetType("System.String") };dt.Columns.Add(dc2);

DataColumn dc3 = new DataColumn() { ColumnName = "changedate", DataType = System.Type.GetType("System.String") };dt.Columns.Add(dc3);

//DataColumn dc4 = new DataColumn() { ColumnName = "changetime", DataType = System.Type.GetType("System.String") };//dt.Columns.Add(dc4);DataColumn dc8 = new DataColumn() { ColumnName = "changetype", DataType = System.Type.GetType("System.String") };dt.Columns.Add(dc8);

DataColumn dc11 = new DataColumn() { ColumnName = "description", DataType = System.Type.GetType("System.String") };dt.Columns.Add(dc11);

DataColumn dc9 = new DataColumn() { ColumnName = "requester", DataType = System.Type.GetType("System.String") };dt.Columns.Add(dc9);

DataColumn dc10 = new DataColumn() { ColumnName = "status", DataType = System.Type.GetType("System.String") };dt.Columns.Add(dc10);

DataColumn dc5 = new DataColumn() { ColumnName = "closuredate", DataType = System.Type.GetType("System.String") };dt.Columns.Add(dc5);

//DataColumn dc6 = new DataColumn() { ColumnName = "closuretime", DataType = System.Type.GetType("System.String") };//dt.Columns.Add(dc6);DataColumn dc7 = new DataColumn() { ColumnName = "approval", DataType = System.Type.GetType("System.String") };dt.Columns.Add(dc7);

return dt;}



The above code when executed looks like :


As seen above only those data are displayed where the current status is "Closed".

No comments:

Post a Comment