protected void btnDaoChu_Click(object sender, EventArgs e)
{
try
{
ThisApplication = new Excel.Application();
m_objBooks = (Excel.Workbooks)ThisApplication.Workbooks;
ThisWorkbook = (Excel._Workbook)(m_objBooks.Add(Type.Missing));
ThisApplication.DisplayAlerts = false;
this.DeleteSheet();
this.AddDatasheet(NewList);
this.LoadData(NewList);
CreateChart();
ThisWorkbook.SaveAs(@"D:\乱七八糟\123.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch
{
}
finally
{
ThisWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
ThisApplication.Workbooks.Close();
ThisApplication.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisWorkbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisApplication);
ThisWorkbook = null;
ThisApplication = null;
GC.Collect();
this.Dispose();
}
}
Excel.Application ThisApplication = null;
Excel.Workbooks m_objBooks = null;
Excel._Workbook ThisWorkbook = null;
Excel.Worksheet xlSheet = null;
Excel.Worksheet xlSheetNew = null;
/// <summary>
/// 用数据填充Sheet
/// </summary>
private void LoadData(List<Idea.Web.Interface.VisitCount.Type.CStat_ViewDay> asList)
{
xlSheet.Cells[1, 1] = "日 期";
xlSheet.Cells[1, 2] = "点击率";
xlSheet.Cells[1, 3] = "IP";
xlSheet.Cells[1, 4] = "独立访客";
xlSheet.Cells[1, 5] = "新访客";
xlSheet.Cells[1, 6] = "访问次数";
xlSheet.Cells[1, 7] = "回访次数";
xlSheet.Cells[1, 8] = "人均PV";
xlSheet.Cells[1, 9] = "PV比例";
for (int i = 0; i < asList.Count; i++)
{
string HangZhi = "";
if (asList[i].vYear != 0 && asList[i].vMonth != 0 && asList[i].vDay != 0)
{
HangZhi = asList[i].vYear + "年" + asList[i].vMonth + "月" + asList[i].vDay + "日";
}
else
{
HangZhi = "总 计";
}
xlSheet.Cells[i + 2, 1] = HangZhi;
xlSheet.Cells[i + 2, 2] = asList[i].pvCount.ToString();
xlSheet.Cells[i + 2, 3] = asList[i].ipCount.ToString();
xlSheet.Cells[i + 2, 4] = asList[i].cusCount.ToString();
xlSheet.Cells[i + 2, 5] = asList[i].newCount.ToString();
xlSheet.Cells[i + 2, 6] = asList[i].timCount.ToString();
xlSheet.Cells[i + 2, 7] = asList[i].otimCount.ToString();
xlSheet.Cells[i + 2, 8] = asList[i].AVG.ToString("0.0");
if (asList[i].pvCount != 0 && asList[0].pvCount != 0)
{
asList[i].Percent = (float)(Convert.ToDouble(asList[i].pvCount) / Convert.ToDouble(asList[0].pvCount)) * 100;
}
xlSheet.Cells[i + 2, 9] = asList[i].Percent.ToString("0.00") + "%";
}
for (int i = 1; i < asList.Count; i++)
{
string HangZhi = "";
if (asList[i].vDay != 0)
{
HangZhi = asList[i].vDay + "日";
}
xlSheetNew.Cells[i, 1] = HangZhi;
xlSheetNew.Cells[i, 2] = asList[i].pvCount.ToString();
}
}
/// <summary>
/// 删除多余的Sheet
/// </summary>
private void DeleteSheet()
{
foreach (Excel.Worksheet ws in ThisWorkbook.Worksheets)
{
if (ws != ThisApplication.ActiveSheet)
{
ws.Delete();
}
}
foreach (Excel.Chart cht in ThisWorkbook.Charts)
{
cht.Delete();
}
}
/// <summary>
/// 创建一个Sheet,用来存数据
/// </summary>
private void AddDatasheet(List<Idea.Web.Interface.VisitCount.Type.CStat_ViewDay> asList)
{
xlSheet = (Excel.Worksheet)ThisWorkbook.Worksheets.Add(Type.Missing, ThisWorkbook.ActiveSheet, Type.Missing, Type.Missing);
xlSheetNew = (Excel.Worksheet)ThisWorkbook.Worksheets.Add(Type.Missing, ThisWorkbook.ActiveSheet, Type.Missing, Type.Missing);
xlSheet.Name = "数据";
xlSheet.Columns.EntireColumn.AutoFit();
}
/// <summary>
/// 创建统计图
/// </summary>
private void CreateChart()
{
Excel.Chart xlChart = (Excel.Chart)ThisWorkbook.Charts.Add(Type.Missing, xlSheetNew, Type.Missing, Type.Missing);
Excel.Range cellRange = (Excel.Range)xlSheetNew.Cells[1, 1];
string RiQiFanWei = "";
RiQiFanWei = "日访问量比较(" + myCStatInfo.StartTime.ToString("yyyy-MM-dd") + " > " + myCStatInfo.EndTime.ToString("yyyy-MM-dd") + ")";
xlChart.ChartWizard(cellRange.CurrentRegion, Excel.XlChartType.xl3DColumn, Type.Missing, Excel.XlRowCol.xlColumns, 1, 0, true, RiQiFanWei, "日期", "访问量", "");
xlChart.Name = "统计";
Excel.ChartGroup grp = (Excel.ChartGroup)xlChart.ChartGroups(1);
grp.GapWidth = 20;
grp.VaryByCategories = true;
Excel.Series s = (Excel.Series)grp.SeriesCollection(1);
s.BarShape = XlBarShape.xlCylinder;
s.HasDataLabels = true;
xlChart.Legend.Position = XlLegendPosition.xlLegendPositionTop;
xlChart.ChartTitle.Font.Size = 24;
xlChart.ChartTitle.Shadow = true;
xlChart.ChartTitle.Border.LineStyle = Excel.XlLineStyle.xlContinuous;
Excel.Axis valueAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlValue, XlAxisGroup.xlPrimary);
valueAxis.AxisTitle.Orientation = -90;
Excel.Axis categoryAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
categoryAxis.AxisTitle.Font.Name = "MS UI Gothic";
//Excel图表导出成图片。
xlChart.Export(@"D:\乱七八糟\123.gif", "gif", xlChart);
}
--摘自互联网,调试通过