PageRenderTime 31ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/XlLateBinding/ReleaseTags/0.7/Examples/Example10/Form1.cs

#
C# | 555 lines | 400 code | 128 blank | 27 comment | 7 complexity | b5c6baccb55aa2fae058ef22c403f936 MD5 | raw file
  1. using System;
  2. using System.Xml;
  3. using System.Data;
  4. using System.Windows.Forms;
  5. using System.Reflection;
  6. using System.Globalization;
  7. using LateBindingApi.Excel;
  8. using LateBindingApi.Excel.Office;
  9. using LateBindingApi.Excel.Charts;
  10. using LateBindingApi.Excel.Styles;
  11. using LateBindingApi.Excel.Enums;
  12. namespace Example10
  13. {
  14. public partial class Form1 : Form
  15. {
  16. /// <summary>
  17. /// month and year to report, no selection dialog in this example
  18. /// the sample data contains datasets from 01/2008 - 06/2010
  19. /// </summary>
  20. private readonly int _yearToReport = 2009;
  21. private readonly int _monthToReport = 5;
  22. #region Fields
  23. XlApplication _excelApplication;
  24. SalesReport _report;
  25. #endregion
  26. #region Construction
  27. public Form1()
  28. {
  29. InitializeComponent();
  30. }
  31. #endregion
  32. #region Gui Trigger
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. // start excel and turn off msg boxes
  36. _excelApplication = new XlApplication();
  37. _excelApplication.DisplayAlerts = false;
  38. _excelApplication.ScreenUpdating = false;
  39. // add a new workbook
  40. XlWorkbook workBook = _excelApplication.Workbooks.Add();
  41. // we use the first sheet as summary sheet and remove the 2 last sheets
  42. XlWorksheet summarySheet = workBook.Worksheets[1];
  43. workBook.Worksheets[3].Delete();
  44. workBook.Worksheets[2].Delete();
  45. // we get the data & perform the report
  46. _report = new SalesReport(_yearToReport, _monthToReport);
  47. _report.Proceed();
  48. // we create named styles for the range.Style property
  49. CreateStorageAndRankingStyle(workBook, "StorageAndRanking");
  50. CreateMonthStyle(workBook, "MonthInfos");
  51. CreateMonthStyle(workBook, "YearTotalInfos");
  52. // write product sheets
  53. XlWorksheet productSheet = null;
  54. foreach (SalesReportProduct itemProduct in _report.Products)
  55. {
  56. productSheet = workBook.Worksheets.Add();
  57. ProceedProductWorksheet(productSheet, itemProduct);
  58. productSheet.Move(null, workBook.Worksheets[workBook.Worksheets.Count]);
  59. }
  60. // write summary sheet
  61. ProceedSummaryWorksheet(_report, workBook,summarySheet, productSheet);
  62. summarySheet.Range("$A2").Select();
  63. // save the book
  64. string fileExtension = XlConverter.GetDefaultExtension(_excelApplication);
  65. string workbookFile = string.Format("{0}\\Example10{1}", Environment.CurrentDirectory, fileExtension);
  66. workBook.SaveAs(workbookFile);
  67. // close excel and dispose reference
  68. _excelApplication.Quit();
  69. _excelApplication.Dispose();
  70. FinishDialog fDialog = new FinishDialog("Workbook saved.", workbookFile);
  71. fDialog.ShowDialog(this);
  72. }
  73. #endregion
  74. #region Write Summary to Worksheet Functions
  75. private void ProceedSummaryMatrix(SalesReport report, XlWorksheet summarySheet, XlStyle matrixStyle)
  76. {
  77. // table columns
  78. summarySheet.Range("B2").Value = "Count";
  79. summarySheet.Range("C2").Value = "Revenue";
  80. summarySheet.Range("D2").Value = "%";
  81. summarySheet.Range("E2").Value = "Storage";
  82. string leftBottomCellAdress = XlConverter.ToCellAdress(1, 3 + report.Products.Length);
  83. string rightBottomCellAdress = XlConverter.ToCellAdress(5, 3 + report.Products.Length);
  84. summarySheet.Range("$A2:$" + rightBottomCellAdress).Style = matrixStyle;
  85. int rowIndex = 3;
  86. int columnIndex = 1;
  87. int i = 0;
  88. foreach (SalesReportProduct itemProduct in report.Products)
  89. {
  90. string prodName = itemProduct.ProductName;
  91. int prodId = itemProduct.ProductId;
  92. summarySheet.Cells(rowIndex, columnIndex).Value = prodName;
  93. string formula = string.Format("='{0}-{1}'!{2}", itemProduct.ProductName, itemProduct.ProductId, XlConverter.ToCellAdress(_monthToReport + 1, 13));
  94. summarySheet.Cells(rowIndex, columnIndex + 1).Value = formula;
  95. formula = string.Format("='{0}-{1}'!{2}", itemProduct.ProductName, itemProduct.ProductId, XlConverter.ToCellAdress(_monthToReport + 1, 12));
  96. summarySheet.Cells(rowIndex, columnIndex + 2).Value = formula;
  97. formula = string.Format("={0}*100/{1}", XlConverter.ToCellAdress(3, rowIndex), XlConverter.ToCellAdress(3, 3 + report.Products.Length));
  98. summarySheet.Cells(rowIndex, columnIndex + 3).Formula = formula;
  99. formula = string.Format("='{0}-{1}'!{2}", itemProduct.ProductName, itemProduct.ProductId, "B6");
  100. summarySheet.Cells(rowIndex, columnIndex + 4).Value = formula;
  101. int storeCount = Convert.ToInt16(summarySheet.Cells(rowIndex, columnIndex + 4).Value);
  102. if( (i%2) == 0)
  103. summarySheet.Range("$A" + (i + 3).ToString() + ":$E" + (i + 3).ToString()).Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Gainsboro);
  104. rowIndex++;
  105. i++;
  106. }
  107. string sumFormula = string.Format("=Sum({0}:{1})", "C3", "C" + (report.Products.Length + 3 - 1).ToString());
  108. summarySheet.Cells(rowIndex, columnIndex + 2).Value = sumFormula;
  109. summarySheet.Range("$C3:$C" + (report.Products.Length+3).ToString()).NumberFormat = "#,##0.00 €";
  110. summarySheet.Range("$D3:$D" + (report.Products.Length + 3).ToString()).NumberFormat = "0\"%\"";
  111. summarySheet.Cells(3 + report.Products.Length,1).Value = "Total:";
  112. summarySheet.Range("D2").HorizontalAlignment = XlHAlign.xlHAlignCenter;
  113. summarySheet.Range("$B2:$E2").Font.Bold = true;
  114. summarySheet.Range(leftBottomCellAdress + ":" + rightBottomCellAdress).Font.Bold = true;
  115. summarySheet.Range(leftBottomCellAdress + ":" + rightBottomCellAdress).BorderAround(XlLineStyle.xlDouble, XlBorderWeight.xlMedium);
  116. }
  117. private void ProceedSummaryWorksheet(SalesReport report, XlWorkbook workBook, XlWorksheet summarySheet, XlWorksheet afterSheet)
  118. {
  119. summarySheet.Name = "Summary";
  120. XlStyle matrixStyle = CreateSummaryStyle(workBook, "MatrixStyle");
  121. ProceedSummaryMatrix(report, summarySheet, matrixStyle);
  122. ProceedSummaryWorksheetCharts(summarySheet, report.Products.Length+1);
  123. ProceedSummaryPrintSettings(summarySheet);
  124. summarySheet.Columns.AutoFit();// proceed AutoFit before header
  125. ProceedSummaryWorksheetHeader(summarySheet);
  126. summarySheet.Select();
  127. }
  128. private void ProceedSummaryWorksheetCharts(XlWorksheet summarySheet, int countOfProducts)
  129. {
  130. string captionRangeAdress = "$A2:$" + XlConverter.ToCellAdress(1, 1 + countOfProducts);
  131. string fieldRangeAdress = "$C2:$" + XlConverter.ToCellAdress(3, 1 + countOfProducts);
  132. double chartTopPosition = summarySheet.Rows[countOfProducts+5].Top;
  133. double chartWidth = summarySheet.Columns[13].Left;
  134. XlChartObject chartSummary = summarySheet.ChartObjects.Add(1, chartTopPosition, chartWidth, 260);
  135. chartSummary.Chart.SetSourceData(summarySheet.Range(captionRangeAdress + ";" + fieldRangeAdress));
  136. fieldRangeAdress = "$D2:$" + XlConverter.ToCellAdress(4, 1 + countOfProducts);
  137. chartTopPosition = summarySheet.Rows[2].Top;
  138. double chartLeftPosition = summarySheet.Columns[8].Left;
  139. double chartHeight = summarySheet.Rows[countOfProducts + 3].Top - chartTopPosition;
  140. chartWidth = summarySheet.Columns[13].Left - summarySheet.Columns[8].Left;
  141. XlChartObject chartPercentOutcome = summarySheet.ChartObjects.Add(chartLeftPosition, chartTopPosition, chartWidth, chartHeight);
  142. chartPercentOutcome.Chart.ChartType = XlChartType.xlPie;
  143. chartPercentOutcome.Chart.SetSourceData(summarySheet.Range(captionRangeAdress + ";" + fieldRangeAdress));
  144. }
  145. private void ProceedSummaryWorksheetHeader(XlWorksheet summarySheet)
  146. {
  147. summarySheet.PageSetup.LeftHeader = "&D created";
  148. summarySheet.PageSetup.CenterHeader = "Vintage Digital Inc.";
  149. summarySheet.PageSetup.RightHeader = string.Format("Monthly Sales Report {1:00}/{0}", _yearToReport, _monthToReport);
  150. }
  151. private void ProceedSummaryPrintSettings(XlWorksheet summarySheet)
  152. {
  153. summarySheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
  154. summarySheet.PageSetup.Zoom = false;
  155. summarySheet.PageSetup.FitToPagesTall = 1;
  156. summarySheet.PageSetup.FitToPagesWide = 1;
  157. }
  158. private XlStyle CreateSummaryStyle(XlWorkbook workBook, string styleName)
  159. {
  160. /*
  161. * borders in styles doesnt realy working, very simple using is possible with the index trick. thats all
  162. */
  163. XlStyle newStyle = workBook.Styles.Add(styleName);
  164. newStyle.Font.Size = 12;
  165. newStyle.Font.Name = "Courier New";
  166. newStyle.Borders[(XlBordersIndex)Constants.xlTop].LineStyle = XlLineStyle.xlContinuous;
  167. newStyle.Borders[(XlBordersIndex)Constants.xlTop].Color = 0;
  168. newStyle.Borders[(XlBordersIndex)Constants.xlTop].Weight = 2;
  169. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlContinuous;
  170. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].Color = 0;
  171. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].Weight = 2;
  172. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlDouble;
  173. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].Color = 0;
  174. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].Weight = 2;
  175. newStyle.Borders[(XlBordersIndex)Constants.xlRight].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlLineStyleNone;
  176. newStyle.Borders[(XlBordersIndex)Constants.xlRight].Color = 0;
  177. newStyle.Borders[(XlBordersIndex)Constants.xlRight].Weight = 2;
  178. return newStyle;
  179. }
  180. #endregion
  181. #region Write Product to Worksheet Functions
  182. private void ProceedProductWorksheet(XlWorksheet productSheet, SalesReportProduct itemProduct)
  183. {
  184. string sheetName = string.Format("{0}-{1}", itemProduct.ProductName, itemProduct.ProductId.ToString());
  185. productSheet.Name = sheetName;
  186. // its not a random chain, write data first and create charts second
  187. ProceedProductStorageInfo(productSheet, itemProduct);
  188. ProceedProductMonthInfo(productSheet, itemProduct);
  189. ProceedProductYearTotalInfo(productSheet, itemProduct);
  190. ProceedProductMonthCharts(productSheet, itemProduct);
  191. ProceedProductPrintSettings(productSheet);
  192. productSheet.Columns.AutoFit(); // proceed AutoFit before header & ranking
  193. ProceedProductWorksheetHeader(productSheet, itemProduct);
  194. ProceedProductRankingInfo(productSheet, itemProduct);
  195. productSheet.Columns[14].ColumnWidth = 2.14;
  196. productSheet.Columns[17].ColumnWidth = 5.14;
  197. }
  198. private void ProceedProductStorageInfo(XlWorksheet productSheet, SalesReportProduct itemProduct)
  199. {
  200. int rowIndex = 3;
  201. int columnIndex = 1;
  202. productSheet.Range("$A3:$B6").StyleAsString = "StorageAndRanking";
  203. productSheet.Cells(rowIndex, columnIndex).Value = "Storage Info";
  204. productSheet.Cells(rowIndex, columnIndex).Font.Bold = true;
  205. productSheet.Cells(rowIndex + 1, columnIndex).Value = "Storage Count";
  206. productSheet.Cells(rowIndex + 2, columnIndex).Value = "Sales in Progress";
  207. productSheet.Cells(rowIndex + 3, columnIndex).Value = "Recalc Storage Count ";
  208. productSheet.Cells(rowIndex + 1, columnIndex + 1).Value = itemProduct.StorageCount;
  209. productSheet.Cells(rowIndex + 2, columnIndex + 1).Value = itemProduct.OpenOrders.CountOfSales;
  210. productSheet.Cells(rowIndex + 3, columnIndex + 1).Value = itemProduct.StorageCount - itemProduct.OpenOrders.CountOfSales;
  211. SetProductStorageCountColor(itemProduct.StorageCount, productSheet.Cells(rowIndex + 1, columnIndex + 1));
  212. SetProductStorageCountColor(itemProduct.StorageCount - itemProduct.OpenOrders.CountOfSales, productSheet.Cells(rowIndex + 3, columnIndex + 1));
  213. }
  214. private void ProceedProductRankingInfo(XlWorksheet productSheet, SalesReportProduct itemProduct)
  215. {
  216. int rowIndex = 3;
  217. int columnIndex = 4;
  218. productSheet.Range("$D3:$F6").StyleAsString = "StorageAndRanking";
  219. productSheet.Cells(rowIndex, columnIndex).Value = "Count Ranking";
  220. productSheet.Cells(rowIndex, columnIndex).Font.Bold = true;
  221. productSheet.Cells(rowIndex + 1, columnIndex).Value = "Month";
  222. productSheet.Cells(rowIndex + 2, columnIndex).Value = "Year";
  223. productSheet.Cells(rowIndex + 3, columnIndex).Value = "Total";
  224. productSheet.Cells(rowIndex + 1, columnIndex + 2).Value = itemProduct.SalesRankMonth;
  225. productSheet.Cells(rowIndex + 2, columnIndex + 2).Value = itemProduct.SalesRankYear;
  226. productSheet.Cells(rowIndex + 3, columnIndex + 2).Value = itemProduct.SalesRankTotal;
  227. productSheet.Range("$D4:$E6").Borders[XlBordersIndex.xlInsideVertical].LineStyle = XlLineStyle.xlLineStyleNone;
  228. }
  229. private void ProceedProductMonthInfo(XlWorksheet productSheet, SalesReportProduct itemProduct)
  230. {
  231. int rowIndex = 9;
  232. int iMonthCellIndex = 1;
  233. productSheet.Range("$A9:$M13").StyleAsString = "MonthInfos";
  234. productSheet.Cells(rowIndex + 1, iMonthCellIndex).Value = "ManufacturerPriceSummary";
  235. productSheet.Cells(rowIndex + 2, iMonthCellIndex).Value = "SalesPricesSummary";
  236. productSheet.Cells(rowIndex + 3, iMonthCellIndex).Value = "TotalRevenue";
  237. productSheet.Cells(rowIndex + 4, iMonthCellIndex).Value = "CountOfSales";
  238. iMonthCellIndex = 2; ;
  239. foreach (SalesReportReportEntity itemMonth in itemProduct.PrevMonths)
  240. {
  241. productSheet.Cells(rowIndex, iMonthCellIndex).Value = GetMonthName(iMonthCellIndex - 2);
  242. productSheet.Cells(rowIndex + 1, iMonthCellIndex).Value = itemMonth.ManufactorPriceSummary;
  243. productSheet.Cells(rowIndex + 2, iMonthCellIndex).Value = itemMonth.SalesPricesSummary;
  244. productSheet.Cells(rowIndex + 3, iMonthCellIndex).Value = itemMonth.OutcomeSummary;
  245. productSheet.Cells(rowIndex + 4, iMonthCellIndex).Value = itemMonth.CountOfSales;
  246. iMonthCellIndex++;
  247. }
  248. string cellAdress1 = XlConverter.ToCellAdress(itemProduct.PrevMonths.Count + 2, 10);
  249. string cellAdress2 = XlConverter.ToCellAdress(itemProduct.PrevMonths.Count + 2, 12);
  250. productSheet.Range("$B10:$" + cellAdress1).Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Gainsboro);
  251. productSheet.Range("$B12:$" + cellAdress2).Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Gainsboro);
  252. productSheet.Cells(rowIndex, iMonthCellIndex).Value = GetMonthName(_monthToReport - 1);
  253. productSheet.Cells(rowIndex + 1, iMonthCellIndex).Value = itemProduct.Month.ManufactorPriceSummary;
  254. productSheet.Cells(rowIndex + 2, iMonthCellIndex).Value = itemProduct.Month.SalesPricesSummary;
  255. productSheet.Cells(rowIndex + 3, iMonthCellIndex).Value = itemProduct.Month.OutcomeSummary;
  256. productSheet.Cells(rowIndex + 4, iMonthCellIndex).Value = itemProduct.Month.CountOfSales;
  257. for (int i = itemProduct.PrevMonths.Count + 2; i <= 12; i++)
  258. {
  259. iMonthCellIndex++;
  260. productSheet.Cells(rowIndex, iMonthCellIndex).Value = GetMonthName(i - 1);
  261. }
  262. productSheet.Range("$B9:$M9").NumberFormat = "";
  263. productSheet.Range("$B9:$M9").Font.Bold = true;
  264. productSheet.Range("$B13:$M13").NumberFormat = "";
  265. productSheet.Range("$B13:$M13").HorizontalAlignment = XlHAlign.xlHAlignCenter;
  266. if (itemProduct.PrevMonths.Count < 11)
  267. {
  268. string topLeftMergeCellAdress = "$" +XlConverter.ToCellAdress(itemProduct.PrevMonths.Count + 3,10);
  269. productSheet.Range(topLeftMergeCellAdress + ":$M13").MergeCells = true;
  270. }
  271. }
  272. private void ProceedProductPrintSettings(XlWorksheet productSheet)
  273. {
  274. productSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
  275. productSheet.PageSetup.Zoom = false;
  276. productSheet.PageSetup.FitToPagesTall = 1;
  277. productSheet.PageSetup.FitToPagesWide = 1;
  278. productSheet.PageSetup.PrintArea = "$A$1:$R$39";
  279. }
  280. private void ProceedProductMonthCharts(XlWorksheet productSheet, SalesReportProduct itemProduct)
  281. {
  282. double chartTop = productSheet.Rows[15].Top;
  283. double chartWidth = productSheet.Columns[14].Left;
  284. double chartHeight = productSheet.Rows[30].Top -productSheet.Rows[15].Top;
  285. XlChartObject chartMonths = productSheet.ChartObjects.Add(1, chartTop, chartWidth, chartHeight);
  286. chartMonths.Chart.SetSourceData(productSheet.Range("$A9:$M12"));
  287. chartTop = productSheet.Rows[31].Top;
  288. chartWidth = productSheet.Columns[14].Left;
  289. chartHeight = productSheet.Rows[40].Top - productSheet.Rows[33].Top;
  290. XlChartObject chartCountMonths = productSheet.ChartObjects.Add(1, chartTop, chartWidth, chartHeight);
  291. chartCountMonths.Chart.ChartType = XlChartType.xlLine;
  292. chartCountMonths.Chart.SetSourceData(productSheet.Range("$A13:$M13"));
  293. double chartLeft = productSheet.Columns[15].Left;
  294. chartTop = productSheet.Rows[15].Top;
  295. chartWidth = productSheet.Columns[19].Left - productSheet.Columns[15].Left;
  296. chartHeight = productSheet.Rows[30].Top - productSheet.Rows[15].Top;
  297. XlChartObject chartCountYears = productSheet.ChartObjects.Add(chartLeft, chartTop, chartWidth, chartHeight);
  298. chartCountYears.Chart.ChartType = XlChartType.xlCylinderColClustered;
  299. chartCountYears.Chart.SetSourceData(productSheet.Range("$O9:$P12"));
  300. }
  301. private void ProceedProductWorksheetHeader(XlWorksheet productSheet, SalesReportProduct itemProduct)
  302. {
  303. int rowIndex = 1;
  304. int columnIndex = 1;
  305. productSheet.PageSetup.LeftHeader = "&D created";
  306. productSheet.PageSetup.CenterHeader = "Vintage Digital Inc.";
  307. productSheet.PageSetup.RightHeader = string.Format("Monthly Sales Report {1:00}/{0}", _yearToReport, _monthToReport);
  308. productSheet.Cells(rowIndex, columnIndex).Value = itemProduct.ProductName;
  309. productSheet.Cells(rowIndex, columnIndex).Font.Bold = true;
  310. productSheet.Cells(rowIndex, columnIndex).Font.Underline = true;
  311. productSheet.Cells(rowIndex, columnIndex).Font.Size = 14;
  312. productSheet.Cells(rowIndex, columnIndex).Font.Name = "Verdana";
  313. }
  314. private void ProceedProductYearTotalInfo(XlWorksheet productSheet, SalesReportProduct itemProduct)
  315. {
  316. int ColumnIndex = 15;
  317. int RowIndex = 9;
  318. productSheet.Range("$O9:$R13").StyleAsString = "YearTotalInfos";
  319. productSheet.Cells(RowIndex, ColumnIndex).Value = "Year " + _yearToReport.ToString();
  320. productSheet.Cells(RowIndex + 1, ColumnIndex).Value = itemProduct.Year.ManufactorPriceSummary;
  321. productSheet.Cells(RowIndex + 2, ColumnIndex).Value = itemProduct.Year.SalesPricesSummary;
  322. productSheet.Cells(RowIndex + 3, ColumnIndex).Value = itemProduct.Year.OutcomeSummary;
  323. productSheet.Cells(RowIndex + 4, ColumnIndex).Value = itemProduct.Year.CountOfSales;
  324. productSheet.Cells(RowIndex, ColumnIndex + 1).Value = "Year " + (_yearToReport - 1).ToString();
  325. productSheet.Cells(RowIndex + 1, ColumnIndex + 1).Value = itemProduct.PrevYear.ManufactorPriceSummary;
  326. productSheet.Cells(RowIndex + 2, ColumnIndex + 1).Value = itemProduct.PrevYear.SalesPricesSummary;
  327. productSheet.Cells(RowIndex + 3, ColumnIndex + 1).Value = itemProduct.PrevYear.OutcomeSummary;
  328. productSheet.Cells(RowIndex + 4, ColumnIndex + 1).Value = itemProduct.PrevYear.CountOfSales;
  329. productSheet.Range("$O10:$P10").Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Gainsboro);
  330. productSheet.Range("$O12:$P12").Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Gainsboro);
  331. ColumnIndex = 18;
  332. RowIndex = 9;
  333. productSheet.Cells(RowIndex, ColumnIndex).Value = "Total";
  334. productSheet.Cells(RowIndex + 1, ColumnIndex).Value = itemProduct.Total.ManufactorPriceSummary;
  335. productSheet.Cells(RowIndex + 2, ColumnIndex).Value = itemProduct.Total.SalesPricesSummary;
  336. productSheet.Cells(RowIndex + 3, ColumnIndex).Value = itemProduct.Total.OutcomeSummary;
  337. productSheet.Cells(RowIndex + 4, ColumnIndex).Value = itemProduct.Total.CountOfSales;
  338. productSheet.Range("$R10").Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Gainsboro);
  339. productSheet.Range("$R12").Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Gainsboro);
  340. productSheet.Range("$O9:$R9").NumberFormat = "";
  341. productSheet.Range("$O9:$R9").Font.Bold = true;
  342. productSheet.Range("$O13:$R13").NumberFormat = "";
  343. productSheet.Range("$O13:$R13").HorizontalAlignment = XlHAlign.xlHAlignCenter;
  344. productSheet.Range("$Q9:$Q13").MergeCells = true;
  345. }
  346. private XlStyle CreateStorageAndRankingStyle(XlWorkbook workBook, string styleName)
  347. {
  348. /*
  349. * borders in styles doesnt realy working, very simple using is possible with the index trick. thats all
  350. */
  351. XlStyle newStyle = workBook.Styles.Add(styleName);
  352. newStyle.Font.Size = 12;
  353. newStyle.Font.Name = "Courier New";
  354. newStyle.Borders[(XlBordersIndex)Constants.xlTop].LineStyle = XlLineStyle.xlContinuous;
  355. newStyle.Borders[(XlBordersIndex)Constants.xlTop].Color = 0;
  356. newStyle.Borders[(XlBordersIndex)Constants.xlTop].Weight = 2;
  357. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlContinuous;
  358. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].Color = 0;
  359. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].Weight = 2;
  360. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlDouble;
  361. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].Color = 0;
  362. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].Weight = 2;
  363. newStyle.Borders[(XlBordersIndex)Constants.xlRight].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlLineStyleNone;
  364. newStyle.Borders[(XlBordersIndex)Constants.xlRight].Color = 0;
  365. newStyle.Borders[(XlBordersIndex)Constants.xlRight].Weight = 2;
  366. return newStyle;
  367. }
  368. private XlStyle CreateMonthStyle(XlWorkbook workBook, string styleName)
  369. {
  370. /*
  371. * borders in styles doesnt realy working, very simple using is possible with the index trick. thats all
  372. */
  373. XlStyle newStyle = workBook.Styles.Add(styleName);
  374. newStyle.Font.Size = 12;
  375. newStyle.Borders[(XlBordersIndex)Constants.xlTop].LineStyle = XlLineStyle.xlContinuous;
  376. newStyle.Borders[(XlBordersIndex)Constants.xlTop].Color = 0;
  377. newStyle.Borders[(XlBordersIndex)Constants.xlTop].Weight = 2;
  378. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlContinuous;
  379. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].Color = 0;
  380. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].Weight = 2;
  381. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlDouble;
  382. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].Color = 0;
  383. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].Weight = 2;
  384. newStyle.Borders[(XlBordersIndex)Constants.xlRight].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlLineStyleNone;
  385. newStyle.Borders[(XlBordersIndex)Constants.xlRight].Color = 0;
  386. newStyle.Borders[(XlBordersIndex)Constants.xlRight].Weight = 2;
  387. newStyle.NumberFormat = "#,##0.00 €";
  388. return newStyle;
  389. }
  390. private XlStyle CreateYearTotalStyle(XlWorkbook workBook, string styleName)
  391. {
  392. /*
  393. * borders in styles doesnt realy working, very simple using is possible with the index trick. thats all
  394. */
  395. XlStyle newStyle = workBook.Styles.Add(styleName);
  396. newStyle.Font.Size = 12;
  397. newStyle.Borders[(XlBordersIndex)Constants.xlTop].LineStyle = XlLineStyle.xlContinuous;
  398. newStyle.Borders[(XlBordersIndex)Constants.xlTop].Color = 0;
  399. newStyle.Borders[(XlBordersIndex)Constants.xlTop].Weight = 2;
  400. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlContinuous;
  401. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].Color = 0;
  402. newStyle.Borders[(XlBordersIndex)Constants.xlBottom].Weight = 2;
  403. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlDouble;
  404. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].Color = 0;
  405. newStyle.Borders[(XlBordersIndex)Constants.xlLeft].Weight = 2;
  406. newStyle.Borders[(XlBordersIndex)Constants.xlRight].LineStyle = LateBindingApi.Excel.Enums.XlLineStyle.xlLineStyleNone;
  407. newStyle.Borders[(XlBordersIndex)Constants.xlRight].Color = 0;
  408. newStyle.Borders[(XlBordersIndex)Constants.xlRight].Weight = 2;
  409. newStyle.NumberFormat = "#,##0.00 €";
  410. return newStyle;
  411. }
  412. private string GetMonthName(int index)
  413. {
  414. return LateBindingApi.Excel.XlLateBindingApiSettings.XlThreadCulture.DateTimeFormat.MonthNames[index].Substring(0, 3);
  415. }
  416. private void SetProductStorageCountColor(int storageCount, XlRange range)
  417. {
  418. if (storageCount <= 50)
  419. range.Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Red);
  420. else if (storageCount <= 100)
  421. range.Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Yellow);
  422. else
  423. range.Interior.Color = XlConverter.ToDouble(System.Drawing.Color.Green);
  424. }
  425. #endregion
  426. }
  427. }