有很大一部分是重複的加上我註解打得很勤勞,所以這次很勤勞的整段貼上來,有附檔,excel在檔案裏面的sample資料夾。
沒有做資料判斷,或任何除錯,請注意~
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChartTest.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fuBtachFile" runat="server" />
<asp:Button ID="btnChart" runat="server" Text="匯圖" OnClick="btnChart_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Visible ="false">
<HeaderStyle BackColor="#996633" ForeColor="White" />
<AlternatingRowStyle BackColor="#ffffcc" />
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="StuNo" HeaderText="學號" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="StuName" HeaderText="學生姓名" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Math1" HeaderText="數學1" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Math2" HeaderText="數學2" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Math3" HeaderText="數學3" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Math4" HeaderText="數學4" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="MathAvg" HeaderText="數學平均" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Chi1" HeaderText="國文1" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Chi2" HeaderText="國文2" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Chi3" HeaderText="國文3" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Chi4" HeaderText="國文4" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="ChiAvg" HeaderText="國文平均" ItemStyle-HorizontalAlign="Center" />
</Columns>
</asp:GridView>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True"
RepeatDirection="Horizontal" Visible ="false"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem>數學</asp:ListItem>
<asp:ListItem>國文</asp:ListItem>
</asp:RadioButtonList>
<asp:Chart ID="cChart" runat="server">
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
<Series>
<asp:Series Name="Series1">
</asp:Series>
</Series>
</asp:Chart>
</div>
</form>
</body>
</html>
後台using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using System.Text.RegularExpressions;
public partial class Default2 : System.Web.UI.Page
{
string Selected = "MathAvg";
protected void Page_Load(object sender, EventArgs e)
{
cChart.Visible = false;
if (!IsPostBack)
{
RadioButtonList1.SelectedValue = "數學";
}
}
protected void btnChart_Click(object sender, EventArgs e)
{
string FileName = System.IO.Path.GetRandomFileName() + ".xls";
string PathName = Server.MapPath(ConfigurationManager.AppSettings["ChartUpload"]) + FileName;
fuBtachFile.PostedFile.SaveAs(PathName);
SetStep2(PathName);
}
private void SetStep2(string PathName)//檢核作業
{
DataTable tUpFileTable;
bool GetUpFileDataIsOK = UpFileDataToDataTable(PathName, out tUpFileTable);
GridView1.DataSource = (DataTable)tUpFileTable;
GridView1.DataBind();
GridView1.Visible = true;
RadioButtonList1.Visible = true;
Session["FileTable"] = tUpFileTable;
CreateChart((DataTable)Session["FileTable"]);
}
private bool UpFileDataToDataTable(string PathName, out DataTable Table)//資料填入table
{
Table = PayBatchNew_EmptyDataTable();
HSSFWorkbook hssfworkbook;
System.IO.FileStream fs = new FileStream(PathName, FileMode.Open);//開檔
hssfworkbook = new HSSFWorkbook(fs);
fs.Close();
HSSFSheet sheet = (HSSFSheet)hssfworkbook.GetSheetAt(0);//選擇excel中第一張表
int rowCount = sheet.LastRowNum;//總列數
int RowStart = sheet.FirstRowNum + 1;//第一列
for (int i = RowStart; i <= sheet.LastRowNum; i++)//將excel中的資料填入datatable
{
HSSFRow row = (HSSFRow)sheet.GetRow(i);
DataRow dataRow = Table.NewRow();
dataRow["StuNo"] = row.GetCell(0).ToString();
dataRow["StuName"] = row.GetCell(1).ToString();
dataRow["Math1"] = row.GetCell(2).ToString();
dataRow["Math2"] = row.GetCell(3).ToString();
dataRow["Math3"] = row.GetCell(4).ToString();
dataRow["Math4"] = row.GetCell(5).ToString();
dataRow["MathAvg"] = Average(row.GetCell(2).ToString(), row.GetCell(3).ToString(), row.GetCell(4).ToString(), row.GetCell(5).ToString());
dataRow["Chi1"] = row.GetCell(7).ToString();
dataRow["Chi2"] = row.GetCell(8).ToString();
dataRow["Chi3"] = row.GetCell(9).ToString();
dataRow["Chi4"] = row.GetCell(10).ToString();
dataRow["ChiAvg"] = Average(row.GetCell(7).ToString(), row.GetCell(8).ToString(), row.GetCell(9).ToString(), row.GetCell(10).ToString());
Table.Rows.Add(dataRow);
}
return true;
}
private DataTable PayBatchNew_EmptyDataTable()//建立table
{
DataTable workTable = new DataTable("ReNewBatchNew");
workTable.Columns.Add("StuNo", typeof(String));//學號
workTable.Columns.Add("StuName", typeof(String));//學生姓名
workTable.Columns.Add("Math1", typeof(int));
workTable.Columns.Add("Math2", typeof(int));
workTable.Columns.Add("Math3", typeof(int));
workTable.Columns.Add("Math4", typeof(int));
workTable.Columns.Add("MathAvg", typeof(float));//數學平均
workTable.Columns.Add("Chi1", typeof(int));
workTable.Columns.Add("Chi2", typeof(int));
workTable.Columns.Add("Chi3", typeof(int));
workTable.Columns.Add("Chi4", typeof(int));
workTable.Columns.Add("ChiAvg", typeof(float));//國文平均
return workTable;
}
public void CreateChart(DataTable upFileTable)
{
string StuName = "StuName";
Chart cChart = new Chart();
cChart.ChartAreas.Add(new ChartArea("Areas"));
cChart.Series.Clear();
cChart.Series.Add(Selected);
cChart.Legends.Add("Legends1");
cChart.Series[Selected].XValueMember = StuName;
cChart.Series[Selected].YValueMembers = Selected;
cChart.DataSource = upFileTable;
cChart.DataBind();
cChart = ChartDesign(cChart, Selected);
this.Page.Form.Controls.Add(cChart);
}
private Chart ChartDesign(Chart cChart, string Selected)
{
//圖的大小
cChart.Width = 800;
cChart.Height = 600;
//加入圖的標題
Title title = new Title();
title.Text = RadioButtonList1.SelectedValue + "長條圖";
title.Alignment = ContentAlignment.MiddleCenter;
title.Font = new System.Drawing.Font("Trebuchet MS", 24F, FontStyle.Underline);
cChart.Titles.Add(title);
//設定長條圖框框的外觀屬性
cChart.ChartAreas["Areas"].Area3DStyle.Enable3D = true;//是否3D
cChart.ChartAreas["Areas"].Area3DStyle.IsClustered = true;//要不要併排
cChart.ChartAreas["Areas"].Area3DStyle.Rotation = 40;//垂直角度
cChart.ChartAreas["Areas"].Area3DStyle.Inclination = 50;//水平角度
cChart.ChartAreas["Areas"].Area3DStyle.PointDepth = 60;//數據條深度
cChart.ChartAreas["Areas"].Area3DStyle.WallWidth = 0;//外牆寬度
cChart.ChartAreas["Areas"].Area3DStyle.LightStyle = LightStyle.Realistic;//光源
cChart.ChartAreas["Areas"].BackColor = Color.FromArgb(240, 240, 240);//背景色
cChart.ChartAreas["Areas"].AxisX2.Enabled = AxisEnabled.True;//X軸上方文字
cChart.ChartAreas["Areas"].AxisY2.Enabled = AxisEnabled.True;//Y軸右邊文字
//Y 軸線顏色
cChart.ChartAreas["Areas"].AxisY.MajorGrid.LineColor = Color.FromArgb(200, 0, 0);
//X 軸線顏色
cChart.ChartAreas["Areas"].AxisX.MajorGrid.LineColor = Color.FromArgb(0, 0, 200);
cChart.ChartAreas["Areas"].AxisY.LabelStyle.Format = "#,###";//Y軸數字格式
//設定右上角小框框的外觀屬性
cChart.Legends["Legends1"].DockedToChartArea = "Areas"; //在圖表內
cChart.Legends["Legends1"].Docking = Docking.Bottom; //設定在圖表內的哪
cChart.Legends["Legends1"].BackColor = Color.FromArgb(200, 200, 200); //背景色
cChart.Legends["Legends1"].BackHatchStyle = ChartHatchStyle.HorizontalBrick;//背景樣式
cChart.Legends["Legends1"].BorderWidth = 1;//框線寬度
cChart.Legends["Legends1"].BorderColor = Color.FromArgb(200, 200, 200);//框線顏色
//設定資料的外觀屬性
cChart.Series[Selected].ChartType = SeriesChartType.Column;//直條圖
//cChart.Series[Selected].ChartType = SeriesChartType.Doughnut;//雷達圖
cChart.Series[Selected].Legend = "Legends1";
cChart.Series[Selected].LegendText = RadioButtonList1.SelectedValue;//下方小框框的文字
cChart.Series[Selected].LabelFormat = "#,###"; //數字格式
cChart.Series[Selected].MarkerSize = 8; //Label 範圍大小
cChart.Series[Selected].LabelForeColor = Color.FromArgb(200, 40, 100); //字體顏色
//字體設定
cChart.Series[Selected].Font = new System.Drawing.Font("Trebuchet MS", 10, System.Drawing.FontStyle.Bold);
cChart.Series[Selected].LabelBackColor = Color.FromArgb(150, 255, 255, 255);//文字背景色
cChart.Series[Selected].Color = Color.FromArgb(240, 65, 140, 240); //資料點背景色
cChart.Series[Selected].IsValueShownAsLabel = true; //是否顯示資料點
Page.Controls.Add(cChart);
return cChart;
}
//radiobutton事件
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (RadioButtonList1.SelectedValue)
{
case "數學":
Selected = "MathAvg";
CreateChart((DataTable)Session["FileTable"]);
break;
default:
Selected = "ChiAvg";
CreateChart((DataTable)Session["FileTable"]);
break;
}
}
private float Average(string one, string two, string three, string four)
{
return (float.Parse(one) + float.Parse(two) + float.Parse(three) + float.Parse(four)) / 4;
}
}
完成圖
檔案:點我下載
沒有留言:
張貼留言