public void ExtractBaseMegTest() { string type = "高风险"; //基金类型 string scale = "37.13"; //规模 FundQueryRT target = new FundQueryRT(); target.GetFundPage("000031");//华夏复兴基金代码 target.ExtractBaseMeg(); Assert.AreEqual(type, target.FundType); Assert.AreEqual(scale, target.Scale); }
public void SaveBaseMessage(List<FundMsgDownload.BaseMsg> funds) { ApplicationClass xlsApp = new ApplicationClass(); //创建Excel应用程序对象的一个实例 Debug.Assert(xlsApp != null); //创建Excel对象 Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application(); objExcel.Visible = false; Object missing = System.Reflection.Missing.Value; //定义缺省值 //创建一个新的工作薄,并引用缺省的工作表 创建Excel后不会自动创建新的工作薄(文件) Workbook objBook = objExcel.Workbooks.Add(missing); Worksheet objSheet = (Worksheet)objBook.Worksheets.get_Item(1); //Range对象用于选择一个或多个单元格 Range objRange; //基金信息开始行 int stateRow = 3; objRange = objSheet.get_Range("A1", "F1"); //合并单元格 objRange.Merge(0); objRange.HorizontalAlignment = XlHAlign.xlHAlignCenter; // objRange = objSheet.get_Range("A1", missing); objRange.Value2 = "基金基本信息"; objRange.Font.Size = 20; objRange.Font.Bold = true; //循环放入列名 int titleIndex = 1; objSheet.Cells[stateRow - 1, titleIndex++] = "基金代码"; objSheet.Cells[stateRow - 1, titleIndex++] = "基金名称缩写"; objSheet.Cells[stateRow - 1, titleIndex++] = "基金名称"; objSheet.Cells[stateRow - 1, titleIndex++] = "基金类型"; //后期加入的代码f objSheet.Cells[stateRow - 1, titleIndex++] = "风险类型"; objSheet.Cells[stateRow - 1, titleIndex++] = "规模"; FundQueryRT fqRT = new FundQueryRT(); //循环放入所有数据 int iRow = stateRow; foreach (var fund in funds) { int icel = 1;//列号 //向单元格中插入数据 objSheet.Cells[iRow, icel++] = "'" + fund.code;//为了实现左对齐 objSheet.Cells[iRow, icel++] = fund.abbreviation; objSheet.Cells[iRow, icel++] = fund.name; objSheet.Cells[iRow, icel++] = fund.type; //后期加入的代码 fqRT.GetFundPage(fund.code); fqRT.ExtractBaseMeg(); objSheet.Cells[iRow, icel++] = fqRT.FundType; // objSheet.Cells[iRow, icel++] = fqRT.Custodian; objSheet.Cells[iRow, icel++] = fqRT.Scale; iRow++; } objRange = objSheet.get_Range("a2", "d2"); objRange.EntireColumn.AutoFit(); //自动调整表格大小 objRange.HorizontalAlignment = XlHAlign.xlHAlignLeft;//将所有单元格左对齐 //在表格底部添加时间 objRange = objSheet.get_Range("a" + (iRow + 2).ToString(), "d" + (iRow + 2).ToString());//选中单元格 objRange.Merge(0);//合并 objRange.Value2 = DateTime.Now.ToString();//把时间赋值给单元格 objRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;//中间对齐 objRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb(); this.excelBaseMsgBook = objBook; objBook.Saved = true;//保存表格 DateTime dtime = DateTime.Now;//获取当前时间 string fileName = dtime.ToString("yyyy-MM-dd hh-mm") + ".xls"; string fileWay = System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\" + fileName; objBook.SaveCopyAs(fileWay);//将表格拷贝到指定路径 objBook.Close(true, Type.Missing, Type.Missing);//关闭表格 objExcel = null;//清空Excel xlsApp.Quit();//释放资源 xlsApp = null; }