private void btnExportData_Click(object sender, EventArgs e) { ExportBuildingDataAssessorFileRequest request = null; ExportBuildingDataAssessorFileResponse response = null; try { if (!(sfdExportData.ShowDialog() == DialogResult.OK)) { throw new RealPropertyTaxProjectionException(Program.SKIP_KEYWORD); } request = new ExportBuildingDataAssessorFileRequest() { SourceFilePath = this.sfdExportData.FileName, BuildingDataAssessors = this.DataSource }; Program.realPropertyTaxProjectionManager = BusinessDelegateFactory.GetInstance().GetRealPropertyTaxProjectionService(); response = Program.realPropertyTaxProjectionManager.ExportBuildingDataAssessorFile(request); if (response.Result.IsSuccess == true) { FormHelper.MessageShow(response.Result.Message); } } catch (RealPropertyTaxProjectionException ex) { if (!ex.Message.Equals(Program.SKIP_KEYWORD)) { FormHelper.MessageShow(ex.Message); } } finally { Program.realPropertyTaxProjectionManager = null; } }
public ExportBuildingDataAssessorFileResponse ExportBuildingDataAssessorFile(ExportBuildingDataAssessorFileRequest request) { ExportBuildingDataAssessorFileResponse response = null; Excel.Application excelApplication = null; try { //Directory.CreateDirectory(Path.GetDirectoryName(request.DataFilePath)); response = new ExportBuildingDataAssessorFileResponse(); //write to excel using (excelApplication = new Excel.Application()) { excelApplication.DisplayAlerts = false; Excel.Workbook workBook = excelApplication.Workbooks.Add(); Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets[1]; workSheet.Name = string.Format("{0} {1}","BldgData Assessor", DateTime.Now.ToString("yyyyMMdd")); int counter = 0; foreach (BuildingDataAssessor buildingDataAssessor in request.BuildingDataAssessors) { counter++; if (counter.Equals(1)) { //add headers continue; } //TODO: add column fields from incoming file //add details workSheet.Cells[counter, 1].Value = "hello 33"; workSheet.Cells[counter, 2].Value = "world"; } //save excel workBook.SaveAs(request.SourceFilePath); //quit excel excelApplication.Quit(); } //set result if (File.Exists(request.SourceFilePath)) { response.Result = new Result() { IsSuccess = true, Message = string.Format("{0}\n{1}", "The data is exported successfully at the following path:", request.SourceFilePath) }; } else { response.Result = new Result() { IsSuccess = false, Message = string.Format("{0}\n{1}", "Fail in exporting data to:", request.SourceFilePath) }; } return response; } finally { response = null; if (excelApplication != null) excelApplication.Dispose(); excelApplication = null; } }