private void readDDPDemandByCollection(Demand demand) { excel = new ExcelUtil(); excel.AddWorkbook(); excel.Worksheet("Sheet1"); String cell = null; String path = null; foreach (String cntr in demand.RowCollection) { cell = demand.DdpDemandkeywordColumn + cntr; path = @"='" + demand.Path + "\\[" + demand.FileName + "]" + demand.WorkSheet + "'!" + cell; excel.WriteCell(1, 1, path); String data = excel.ReadCell("A1").ToString(); dataType = new DataType(data); dataType = (DataType)dataTypeSessionData.createIfNotExist(dataType); Area area = new Area("Luzon"); area = (Area)areaSessionData.createIfNotExist(area); Regions region = new Regions(demand.Region.ToString().Replace("_", " "), area.areaId); region = (Regions)regionSessionData.createIfNotExist(region); Cooperative cooperative = new Cooperative(demand.Cooperative, demand.CooperativeAccronym, region.regionId); cooperative = (Cooperative)cooperativeSessionData.createIfNotExist(cooperative); CooperativeDataValues coopSearch = cooperativeDataValueSessionData.findDataValuesByCoopertiveId(cooperative, dataType); if (coopSearch != null) { cooperativeDataContentSessionData.deleteByDataValueId(coopSearch.cooperativeDataValuesId); cooperativeDataValue = coopSearch; } else { cooperativeDataValue = new CooperativeDataValues(cooperative, dataType); cooperativeDataValue = (CooperativeDataValues)cooperativeDataValueSessionData.add(cooperativeDataValue); } writeDDPDemandCoopCollection(demand, cntr); } excel.Close(); }
private void button8_Click(object sender, EventArgs e) { excel = new ExcelUtil(); excel.AddWorkbook(); excel.Worksheet("Sheet1"); List <String> lstData = new List <String>(); StreamWriter writer = new StreamWriter("testCSV.csv", false); List <String> luzonPSASummary = new List <String>(); List <String> luzonDemandSummary = new List <String>(); List <String> mindanaoPSASummary = new List <String>(); List <String> mindanaoDemandSummary = new List <String>(); List <String> visayasPSASummary = new List <String>(); List <String> visayasDemandSummary = new List <String>(); //testing for changes row = 0; int areaCntr = 0; IList <Area> areas = areaSessionData.getAllAreas(); foreach (Area area in areas) { areaCntr++; foreach (Regions region in regionSessionData.getAllRegionsByArea(area)) { List <Cooperative> coops = cooperativeSessionData.getCooperativeByRegion(region.regionId); int coopCntr = 0; foreach (Cooperative coop in coops) { coopCntr++; row++; writer.WriteLine(); row++; writer.WriteLine(area.name + "," + region.name + "," + coop.name); IList <Plant> plants = plantSessionData.getAllPlantByCoop(coop.cooperativeId); //row++; //writer.WriteLine(area.name + "," + region.name + ",PSA,=SUM(D" + (row + 1) + ":D" + (row + plants.Count) + ")"); int PSARow = row; //row++; int col; //Console.WriteLine(plants.Count.ToString()); Console.WriteLine("Coop: " + coop.name + " " + ((double)coopCntr / coops.Count)); txtOutput.AppendText(areaCntr + " out of" + areas.Count + ": " + (((double)coopCntr / coops.Count) * 100) + "%\n"); loadingBar.Value = (int)(((double)coopCntr / coops.Count) * 100); int colCntr; int psaRowMarker = 0; List <String> data1; List <String> data2; bool psaRegistered = false; foreach (Plant plant in plants) { if (plant != null) { col = 3; colCntr = 1; // data1 = new List <String>(); data2 = new List <String>(); String psaOutput = ""; foreach (DataValues dataValue in plant.dataValues) { col++; List <string> tempData = null; if (colCntr == 1) { data1 = dataContentSessionData.findDataContentValueByDataValuesId(dataValue); tempData = data1; } else if (colCntr == 2) { data2 = dataContentSessionData.findDataContentValueByDataValuesId(dataValue); tempData = data2; } if (!psaRegistered) { int column = 4; foreach (String str in tempData) { psaOutput += "=SUM(" + numUtil.getLetterByNumber(column) + ("row") + ":" + numUtil.getLetterByNumber(column) + ("rows") + "),"; column++; } } colCntr++; col = 4; } if (!psaRegistered) { row++; psaRowMarker = row; if (area.name.Equals("Luzon")) { luzonPSASummary.Add(row.ToString()); } else if (area.name.Equals("Mindanao")) { mindanaoPSASummary.Add(row.ToString()); } else if (area.name.Equals("Visayas")) { visayasPSASummary.Add(row.ToString()); } psaOutput.Remove(psaOutput.LastIndexOf(',')); writer.WriteLine(area.name + "," + region.name + ",PSA," + psaOutput.Replace("rows", (row + plants.Count).ToString()).Replace("row", (row + 1).ToString())); } psaRegistered = true; List <String> output = merge(data1, data2); output.Insert(0, area.name.Replace(",", "")); output.Insert(1, region.name); output.Insert(2, plant.name.Replace(",", "")); Console.WriteLine(String.Join(",", output)); row++; writer.WriteLine(String.Join(",", output)); } } Console.WriteLine("-----------"); col = 3; colCntr = 1; data1 = new List <String>(); data2 = new List <String>(); foreach (CooperativeDataValues dataValue in coop.cooperativeDataValues) { List <string> tempData = null; col++; if (colCntr == 1) { data1 = cooperativeDataContentSessionData.findDataContentValueByDataValuesId(dataValue); tempData = data1; } else if (colCntr == 2) { data2 = cooperativeDataContentSessionData.findDataContentValueByDataValuesId(dataValue); tempData = data2; } colCntr++; } List <String> output2 = merge(data1, data2); int resColumn = 4; row++; String reserveDef = ""; foreach (String str in output2) { reserveDef += "=" + (psaRowMarker == 0 ? "0" : numUtil.getLetterByNumber(resColumn) + (psaRowMarker)) + "-" + numUtil.getLetterByNumber(resColumn) + (row) + ","; resColumn++; } output2.Insert(0, area.name.Replace(",", "")); output2.Insert(1, region.name); output2.Insert(2, "DEMAND"); if (area.name.Equals("Luzon")) { luzonDemandSummary.Add(row.ToString()); } else if (area.name.Equals("Mindanao")) { mindanaoDemandSummary.Add(row.ToString()); } else if (area.name.Equals("Visayas")) { visayasDemandSummary.Add(row.ToString()); } row++; writer.WriteLine(String.Join(",", output2)); if (!reserveDef.Equals("")) { reserveDef.Remove(reserveDef.LastIndexOf(',')); } writer.WriteLine(area.name + "," + region.name + ",RESERVE/DEFICIT," + reserveDef); } } } writer.Close(); //ExcelUtil excel = new ExcelUtil(); row++; row++; excel.Open(Application.StartupPath.ToString() + "\\testCSV.csv"); excel.Worksheet("testCSV"); excel.WriteCell(2, 1, "Area"); excel.WriteCell(2, 2, "Region"); for (int index = 0; index < luzonPSASummary.Count; index++) { excel.setBackgroundColor("A" + luzonPSASummary[index].ToString() + ":AG" + luzonPSASummary[index].ToString(), Color.Aqua); } for (int index = 0; index < luzonDemandSummary.Count; index++) { excel.setBackgroundColor("A" + luzonDemandSummary[index].ToString() + ":AG" + luzonDemandSummary[index].ToString(), Color.Wheat); excel.setBackgroundColor("A" + (Int32.Parse(luzonDemandSummary[index].ToString()) + 1) + ":AG" + (Int32.Parse(luzonDemandSummary[index].ToString()) + 1), Color.Gainsboro); } for (int index = 0; index < mindanaoPSASummary.Count; index++) { excel.setBackgroundColor("A" + mindanaoPSASummary[index].ToString() + ":AG" + mindanaoPSASummary[index].ToString(), Color.Aqua); } for (int index = 0; index < mindanaoDemandSummary.Count; index++) { excel.setBackgroundColor("A" + mindanaoDemandSummary[index].ToString() + ":AG" + mindanaoDemandSummary[index].ToString(), Color.Wheat); excel.setBackgroundColor("A" + (Int32.Parse(mindanaoDemandSummary[index].ToString()) + 1) + ":AG" + (Int32.Parse(mindanaoDemandSummary[index].ToString()) + 1), Color.Gainsboro); } for (int index = 0; index < visayasPSASummary.Count; index++) { excel.setBackgroundColor("A" + visayasPSASummary[index].ToString() + ":AG" + visayasPSASummary[index].ToString(), Color.Aqua); } for (int index = 0; index < visayasPSASummary.Count; index++) { excel.setBackgroundColor("A" + visayasPSASummary[index].ToString() + ":AG" + visayasPSASummary[index].ToString(), Color.Wheat); excel.setBackgroundColor("A" + (Int32.Parse(visayasPSASummary[index].ToString()) + 1) + ":AG" + (Int32.Parse(visayasPSASummary[index].ToString()) + 1), Color.Gainsboro); } int colHeader = 4; int colDataHeader = 2008; for (int yr = 2008; yr <= 2022 + (2022 - 2008); yr++) { if (yr % 2 == 0) { excel.WriteCell(1, colHeader, colDataHeader.ToString()); excel.Merge(numUtil.getLetterByNumber(colHeader) + "1", numUtil.getLetterByNumber(colHeader + 1) + "1"); excel.WriteCell(2, colHeader, "PSA DEMAND"); excel.WriteCell(2, colHeader + 1, "E. SALES"); colDataHeader++; } colHeader++; } excel.filter("A1", "B" + row); excel.WriteCell(row, 2, "Luzon"); excel.WriteCell(row, 3, "PSA"); excel.WriteCell(row, 4, Summary(luzonPSASummary).Replace(",", ",D")); excel.setBackgroundColor("D" + row, Color.Gainsboro); excel.copy("D" + row, "E" + row + ":AG" + row); row++; excel.WriteCell(row, 2, "Luzon"); excel.WriteCell(row, 3, "DEMAND"); excel.WriteCell(row, 4, Summary(luzonDemandSummary).Replace(",", ",D")); excel.setBackgroundColor("D" + row, Color.LightCyan); excel.copy("D" + row, "E" + row + ":AG" + row); row++; excel.WriteCell(row, 2, "Luzon"); excel.WriteCell(row, 3, "RESERVE/DEFICIT"); excel.WriteCell(row, 4, "=D" + (row - 2) + "-D" + (row - 1)); excel.setBackgroundColor("D" + row, Color.LightCyan); excel.copy("D" + row, "E" + row + ":AG" + row); row++; row++; excel.WriteCell(row, 2, "Visayas"); excel.WriteCell(row, 3, "PSA"); excel.WriteCell(row, 4, Summary(visayasPSASummary).Replace(",", ",D")); excel.setBackgroundColor("D" + row, Color.Gainsboro); excel.copy("D" + row, "E" + row + ":AG" + row); row++; excel.WriteCell(row, 2, "Visayas"); excel.WriteCell(row, 3, "DEMAND"); excel.WriteCell(row, 4, Summary(visayasDemandSummary).Replace(",", ",D")); excel.setBackgroundColor("D" + row, Color.LightCyan); excel.copy("D" + row, "E" + row + ":AG" + row); row++; excel.WriteCell(row, 2, "Visayas"); excel.WriteCell(row, 3, "RESERVE/DEFICIT"); excel.WriteCell(row, 4, "=D" + (row - 2) + "-D" + (row - 1)); excel.setBackgroundColor("D" + row, Color.LightCyan); excel.copy("D" + row, "E" + row + ":AG" + row); row++; row++; excel.WriteCell(row, 2, "Mindanao"); excel.WriteCell(row, 3, "PSA"); excel.WriteCell(row, 4, Summary(mindanaoPSASummary).Replace(",", ",D")); excel.setBackgroundColor("D" + row, Color.Gainsboro); excel.copy("D" + row, "E" + row + ":AG" + row); row++; excel.WriteCell(row, 2, "Mindanao"); excel.WriteCell(row, 3, "DEMAND"); excel.WriteCell(row, 4, Summary(mindanaoDemandSummary).Replace(",", ",D")); excel.setBackgroundColor("D" + row, Color.LightCyan); excel.copy("D" + row, "E" + row + ":AG" + row); row++; excel.WriteCell(row, 2, "Mindanao"); excel.WriteCell(row, 3, "RESERVE/DEFICIT"); excel.WriteCell(row, 4, "=D" + (row - 2) + "-D" + (row - 1)); excel.setBackgroundColor("D" + row, Color.LightCyan); excel.Format("A3:AG" + row, "#,##0.00"); excel.copy("D" + row, "E" + row + ":AG" + row); excel.Save("newExcel.xls"); GC.Collect(); Console.WriteLine("Save!"); }
private void button7_Click(object sender, EventArgs e) { Demand demand = (Demand)propertyGrid.SelectedObject; NumberToLetterUtil converter = new NumberToLetterUtil(); demand.PscEcsData.Clear(); demand.PscEcsData.Add(new PscEcsData() { Name = "2008", Column = "J" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2009", Column = "K" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2010", Column = "L" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2011", Column = "M" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2012", Column = "N" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2013", Column = "O" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2014", Column = "P" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2015", Column = "Q" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2016", Column = "R" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2017", Column = "S" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2018", Column = "T" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2019", Column = "U" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2020", Column = "V" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2021", Column = "W" }); demand.PscEcsData.Add(new PscEcsData() { Name = "2022", Column = "X" }); NumberToLetterUtil numUtil = new NumberToLetterUtil(); if (demand.RowSequenceType == RowSequenceType.Range) { try { String firstLetter = demand.RowRangeFrom.ElementAt(0).ToString(); ExcelUtil excel = new ExcelUtil(); excel.Open(demand.Path + "/" + demand.FileName); excel.Worksheet(demand.WorkSheet); Object[,] obj = excel.ReadCellByRange(demand.RowRangeFrom + ":" + demand.RowRangeTo); for (int row = 1; row <= obj.GetUpperBound(0); row++) { //for (int col = 1; col <= obj.GetUpperBound(1); col++) { // if (obj[row, col] == null) { // Console.WriteLine("NUl"); // continue; // } // Console.WriteLine(obj[row, col].ToString()); //} int numberGap = Int32.Parse(numUtil.getNumberByLetter(firstLetter)); //Area area = new Area(obj[row, Int32.Parse(numUtil.getNumberByLetter(demand.AreaColumn)) - numberGap + 1].ToString()); Area area = new Area("Luzon"); area = (Area)areaSessionData.createIfNotExist(area); Regions region = new Regions(obj[row, Int32.Parse(numUtil.getNumberByLetter(demand.RegionColumn)) - numberGap + 1].ToString(), area.areaId); region = (Regions)regionSessionData.createIfNotExist(region); Cooperative cooperative = new Cooperative(obj[row, Int32.Parse(numUtil.getNumberByLetter(demand.CooperativeColumn)) - numberGap + 1].ToString(), "", region.regionId); cooperative = (Cooperative)cooperativeSessionData.createIfNotExist(cooperative); if (obj[row, Int32.Parse(numUtil.getNumberByLetter(demand.PlantColumn)) - numberGap + 1] == null) { continue; } Plant plant = new Plant(obj[row, Int32.Parse(numUtil.getNumberByLetter(demand.PlantColumn)) - numberGap + 1].ToString(), cooperative.cooperativeId); plant = (Plant)plantSessionData.createIfNotExist(plant); //plant = (Plant)plantSessionData.add(plant); //if (plant == null) continue; Console.WriteLine("Plant: " + plant.plantId); DataType dataType = new DataType(obj[row, Int32.Parse(numUtil.getNumberByLetter(demand.PscEcsKeywordColumn)) - numberGap + 1].ToString()); dataType = (DataType)dataTypeSessionData.createIfNotExist(dataType); DataValues dataValue = new DataValues(plant.plantId, dataType.dataTypeId); dataValue = (DataValues)dataValuesSessionData.createIfNotExist(dataValue); foreach (PscEcsData ecsData in demand.PscEcsData) { object dataObect = obj[row, Int32.Parse(numUtil.getNumberByLetter(ecsData.Column)) - numberGap + 1]; String data = "0"; if (dataObect != null) { data = dataObect.ToString(); } DataContent dataContent = new DataContent(ecsData.Name, data, dataValue.dataValuesId); dataContentSessionData.add(dataContent); } } excel.Close(); } catch (WorksheetNotFoundException wnfe) { MessageBox.Show(null, wnfe.Message, "Error Window", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (RangeInvalidException rie) { MessageBox.Show(null, rie.Message, "Error Window", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (IndexOutOfRangeException iore) { MessageBox.Show(null, "Index not in a range.", "Error Window", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (demand.RowSequenceType == RowSequenceType.Collection) { try { excel = new ExcelUtil(); excel.AddWorkbook(); excel.Worksheet("Sheet1"); String cell = null; String path = null; foreach (String row in demand.RowCollection) { cell = demand.PlantColumnIndex + row; path = @"='" + demand.Path + "\\[" + demand.FileName + "]" + demand.WorkSheet + "'!" + cell; excel.WriteCell(1, 1, path); String plantName = excel.ReadCell("A1").ToString(); Console.WriteLine(plantName); int rowIndex = Int32.Parse(row) + 2; for (int cntr = rowIndex; cntr < (rowIndex + 2); cntr++) { cell = demand.SupplyContractedKeywordColumn + cntr; path = @"='" + demand.Path + "\\[" + demand.FileName + "]" + demand.WorkSheet + "'!" + cell; excel.WriteCell(1, 1, path); String data = excel.ReadCell("A1").ToString(); dataType = new DataType(data); dataType = (DataType)dataTypeSessionData.createIfNotExist(dataType); Console.WriteLine("data: " + dataType.dataTypeId); Area area = new Area("Luzon"); area = (Area)areaSessionData.createIfNotExist(area); Regions region = new Regions(demand.SupplyContractedRegion.ToString().Replace("_", " "), area.areaId); region = (Regions)regionSessionData.createIfNotExist(region); Cooperative cooperative = new Cooperative(demand.SupplyContractedCooperative, demand.SupplyContractedCooperativeAccronym, region.regionId); cooperative = (Cooperative)cooperativeSessionData.createIfNotExist(cooperative); Plant plant = new Plant(plantName, cooperative.cooperativeId); plant = (Plant)plantSessionData.createIfNotExist(plant); Console.WriteLine("Plant: " + plant.plantId); //DataValues coopSearch = data DataValues coopSearch = dataValuesSessionData.findDataValuesByPlantId(plant, dataType); if (coopSearch != null) { dataContentSessionData.deleteByDataValueId(coopSearch.dataValuesId); dataValue = coopSearch; } else { dataValue = new DataValues(); dataValue.plantId = plant.plantId; dataValue.dataTypeId = dataType.dataTypeId; dataValue = (DataValues)dataValuesSessionData.add(dataValue); } foreach (DDPSupplyContractedData contractedData in demand.SupplyContractedData) { cell = contractedData.Column + cntr; path = @"='" + demand.Path + "\\[" + demand.FileName + "]" + demand.WorkSheet + "'!" + cell; excel.WriteCell(1, 1, path); Console.WriteLine(contractedData.Name + " - " + excel.ReadCell(1, "A")); dataContent = new DataContent(); dataContent.header = contractedData.Name; dataContent.value = excel.ReadCell("A1"); dataContent.dataValuesId = dataValue.dataValuesId; dataContentSessionData.add(dataContent); } } } excel.Close(); } catch (NullReferenceException nre) { MessageBox.Show(null, nre.Message.ToString(), "Error Window", MessageBoxButtons.OK, MessageBoxIcon.Error); } } Console.WriteLine("Success!"); Console.WriteLine(demand.Region); }
private void button2_Click(object sender, EventArgs e) { ExcelUtil excel = new ExcelUtil(); //foreach(Area areas in areaSessionData.getAllRegionsByArea(Area.LUZON)){ // foreach (Regions region in areas.regions) { // // Console.WriteLine(region.name); // } // //excel.WriteCell(1, rows++, region.name); // //foreach (Regions region in areas.regions) { // // Console.WriteLine(region.name); // // excel.WriteCell(1,rows++, region.name); // //} //} //int rows = 1; //int currentRow = 1; //foreach (Area area in areaSessionData.getAllAreas()) { // Console.WriteLine(area.name); // foreach (Regions region in area.regions) { // Console.WriteLine(" +" + region.name); // foreach (Cooperative coop in cooperativeDataSession.getCooperativeByRegion(region.regionId)) { // Console.WriteLine(" +" + coop.name); // foreach (Plant plant in plantDataSession.getAllPlantByCoop(coop.cooperativeId)) { // Console.WriteLine(" +" + plant.name); // } // } // } //} //foreach(Plant data in plantDataSession.getAllPlantData()){ // Console.WriteLine(data.name); // excel.WriteCell(currentRow, 1, data.cooperative.region.area.name); // excel.WriteCell(currentRow, 2, data.cooperative.region.name); // excel.WriteCell(currentRow, 3, data.cooperative.name); // ++currentRow; // //currentRow++; // excel.WriteCell(currentRow, 1, data.cooperative.region.area.name); // excel.WriteCell(currentRow, 2, data.cooperative.region.name); // excel.WriteCell(currentRow, 3, data.name); // foreach (DataValues d in data.dataValues) { // excel.WriteCell(currentRow, 4, d.d2008.ToString()); // excel.WriteCell(currentRow, 6, d.d2009.ToString()); // excel.WriteCell(currentRow, 8, d.d2010.ToString()); // excel.WriteCell(currentRow, 10, d.d2011.ToString()); // excel.WriteCell(currentRow, 12, d.d2012.ToString()); // excel.WriteCell(currentRow, 14, d.d2013.ToString()); // excel.WriteCell(currentRow, 16, d.d2014.ToString()); // excel.WriteCell(currentRow, 18, d.d2015.ToString()); // excel.WriteCell(currentRow, 20, d.d2016.ToString()); // excel.WriteCell(currentRow, 22, d.d2017.ToString()); // excel.WriteCell(currentRow, 24, d.d2018.ToString()); // excel.WriteCell(currentRow, 26, d.d2019.ToString()); // excel.WriteCell(currentRow, 28, d.d2020.ToString()); // excel.WriteCell(currentRow, 30, d.d2021.ToString()); // excel.WriteCell(currentRow, 32, d.d2022.ToString()); // } // //currentRow = rows++; // currentRow += 2; //} //excel.AddWorkbook(); //excel.Worksheet("Sheet1"); //excel.AddWorksheet("Sheet1"); excel.AddWorkbook(); excel.Worksheet("Sheet1"); excel.WriteCell(1, 1, @"='E:\ECs (108)\1. LUZON (50)\2013 DDP REGION 5 (11)\2013 ALECO DDP\[2013_aleco_ddp.xlsm]DDP-Demand'!$F$130"); Console.WriteLine(excel.ReadCell(1, "A")); //excel.Save("test2.xlsx"); excel.Close(); Console.WriteLine("Success!"); }