/// <summary> /// 功能:导出Excel非表格 /// 作者:付强 /// 日期:2013年7月9日 /// </summary> /// <param name="fileName">要保存的文件路径及名称</param> /// <param name="reportSource">数据源</param> private void ExportExcel(string fileName, PageReport reportSource) { Microsoft.Office.Interop.Excel._Application xlsApp = new ApplicationClass(); if (xlsApp == null) { return; } Workbook xlsBook = xlsApp.Workbooks.Add(true); Worksheet xlSheet = (Worksheet)xlsBook.Worksheets[1]; xlSheet.Cells[1, 1] = reportSource.ReportName; Range range = xlSheet.get_Range(xlsApp.Cells[1, 1], xlsApp.Cells[1, 11]); range.MergeCells = true; range.Font.Size = 24; range.Font.Bold = true; range.HorizontalAlignment = XlHAlign.xlHAlignCenter; for (int i = 0; i < reportSource.RowReports.Length; i++) { RowReport item = reportSource.RowReports[i] as RowReport; if (!string.IsNullOrEmpty(item.Field)) { xlSheet.Cells[(i + 1) * 2, 1] = reportSource.RowReports[i].ToString(); range = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[i + 2, 1]; range.Interior.ColorIndex = 15; range.Font.Bold = true; range.HorizontalAlignment = XlHAlign.xlHAlignRight; range.Borders.LineStyle = XlLineStyle.xlLineStyleNone; range = xlSheet.get_Range(xlsApp.Cells[(i + 1) * 2, 1], xlsApp.Cells[(i + 2) * 2 - 1, 1]); range.MergeCells = true; range = xlSheet.get_Range(xlsApp.Cells[i + 2, 2], xlsApp.Cells[i + 2, 11]); range.MergeCells = true; range.Font.Underline = true; range = xlSheet.get_Range(xlsApp.Cells[(i + 1) * 2, 2], xlsApp.Cells[(i + 2) * 2 - 1, 11]); range.MergeCells = true; range.Font.Underline = true; if (!string.IsNullOrEmpty(item.RightContent)) { xlSheet.Cells[(i + 1) * 2, 12] = item.RightContent; range = xlSheet.get_Range(xlsApp.Cells[(i + 1) * 2, 12], xlsApp.Cells[(i + 2) * 2 - 1, 13]); range.HorizontalAlignment = item.ContentAlignment; } } } xlsBook.Saved = true; xlsBook.SaveCopyAs(fileName); }
/// <summary> /// 填空报表专用(获取右标题大小) /// </summary> /// <param name="p_info"></param> /// <returns></returns> private SizeF RightMeasureString(RowReport p_info) { SizeF m_sf = new SizeF(); if (p_info.RightLength > 0) { m_sf = MeasureString(p_info.RightContent, p_info.RightFont, p_info.RightLength); } else { m_sf = MeasureString(p_info.RightContent, p_info.RightFont); } return(m_sf); }
/// <summary> /// 填空报表专用(获取绑定内容) /// </summary> /// <param name="p_info"></param> /// <returns></returns> private string GetRowContent(object p_DataSource, RowReport p_info) { string m_Content = string.Empty; if (p_DataSource != null) { DataTable dt = p_DataSource as DataTable; if (dt.Rows.Count == 1) { if (p_info.Field != string.Empty) { object m_obj = dt.Rows[0][p_info.Field]; if (m_obj != null) { m_Content = m_obj.ToString(); } } } } return(m_Content); }
static void Main(string[] args) { #region 1 //bool CalcAgain = false; //do //{ // var calc = new Calculator(); // calc.SetOperation(); // calc.SetValue1(); // calc.SetValue2(); // calc.ShowResult(); // string inputAnswer; // do // { // Console.WriteLine("Do you want to continue again (Y/N)?"); // inputAnswer = Console.ReadLine(); // } while (inputAnswer.ToUpper() != "Y" && inputAnswer.ToUpper() != "N"); // CalcAgain = inputAnswer == "Y"; //} while (CalcAgain); #endregion #region 2 bool repeat = true; int totalStudents = 0; do { try { Console.Write("Enter Total Students : "); totalStudents = Int32.Parse(Console.ReadLine()); repeat = false; } catch (Exception ex) { Console.WriteLine(ex.Message); repeat = true; } } while (repeat); RowReport[] report = new RowReport[totalStudents]; for (int i = 0; i < totalStudents; i++) { Console.Write("Enter Student Name : "); string name = Console.ReadLine(); int total = 0; string[] subjects = new string[] { "English", "Math", "Computer" }; for (int j = 0; j < subjects.Length; j++) { repeat = true; do { try { Console.Write($"Enter {subjects[j]} Marks (Out Of 100) : "); int marks = Int32.Parse(Console.ReadLine()); if (marks > 100) { throw new InvalidOperationException("The marks is the bigger than 100"); } total += marks; repeat = false; } catch (Exception ex) { Console.WriteLine(ex.Message); repeat = true; } } while (repeat); } report[i] = new RowReport(name, total); } report.OrderByDescending(x => x.Score); Console.WriteLine("Report Card"); for (int i = 0; i < report.Length; i++) { Console.WriteLine($"Student Name: {report[i].StudentName}, Position: {i+1}, Total: {report[i].Score}/300"); } #endregion }