using Excel = Microsoft.Office.Interop.Excel;
namespace PrintProject.ChuangKou
{
public partial class Chart
{
List<string> txDataPie = new List<string>() { "不合格", "合格" };
List<int> tyDataPie=new List<string>() {826,185};
List<string> txDataColumn=new List<string>() {"AAA","BBB","CCC"};
List<int> tyDataNo=new List<string>() {0,0,641};
List<int> tyDataOk=new List<string>() {0,0,185};
private void btn_out_Click(object sender, EventArgs e)
{
//申明保存對話框
SaveFileDialog dlg = new SaveFileDialog();
//默然文件後綴
dlg.DefaultExt = "xls ";
//文件後綴列表
dlg.Filter = "EXCEL文件(*.XLS)|*.xls ";
//默然路徑是系統當前路徑
dlg.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
//打開保存對話框
if (dlg.ShowDialog() == DialogResult.Cancel) return;
//返回文件路徑
string fileNameString = dlg.FileName;
//驗證strFileName是否爲空或值無效
if (fileNameString.Trim() == " ")
{ return; }
Excel.Application objExcel = null;
Excel.Workbook objWorkbook = null;
Excel.Worksheet objsheet = null;
try
{
//申明對象
objExcel = new Excel.Application();
objWorkbook = objExcel.Workbooks.Add(Missing.Value);
objsheet = (Excel.Worksheet)objWorkbook.ActiveSheet;
//合格率
objExcel.Cells[1, 1] = "開始時間";
objExcel.Cells[1,2] = dateTimePicker2.Text;
objExcel.Cells[1, 3] = "結束時間";
objExcel.Cells[1,4] = dateTimePicker2.Text;
objExcel.Cells[2, 1] = "不合格";
objExcel.Cells[3, 1] = "合格";
objExcel.Cells[2,2] = tyDataPie[0];
objExcel.Cells[3,2] = tyDataPie[1];
//餅圖
Excel.Range oResizeRange;
Excel.Chart xlChart = (Excel.Chart)objWorkbook.Charts.Add(Type.Missing, objsheet, Type.Missing, Type.Missing);
xlChart.ChartType= Excel.XlChartType.xlPie;//設置圖形
xlChart.SetSourceData(objsheet.get_Range("A2", "B3"),Excel.XlRowCol.xlColumns);
objWorkbook.ActiveChart.Location(Excel.XlChartLocation.xlLocationAutomatic, "合格率");
objWorkbook.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject, objsheet.Name);
oResizeRange = (Excel.Range)objsheet.Rows.get_Item(7, Missing.Value);
objsheet.Shapes.Item("Chart 1").Top = 70; //調圖表的位置上邊距
objsheet.Shapes.Item("Chart 1").Left = (float)(double)oResizeRange.Left;
objsheet.Shapes.Item("Chart 1").Width = 200; //調圖表的寬度
objsheet.Shapes.Item("Chart 1").Height = 150; //調圖表的高度
#region 管理人員
int col = 6;
objExcel.Cells[2, col] = "用戶名";
objExcel.Cells[2, col+1] = "合格";
objExcel.Cells[2, col + 2] = "不合格";
int row = 3;
for (int i=0;i< txDataColumn.Count;i++) {
objExcel.Cells[row, col] = txDataColumn[i];
row++;
}
row = 3;
for (int i = 0; i < tyDataOk.Count; i++)
{
objExcel.Cells[row, col+1] = tyDataOk[i];
row++;
}
row = 3;
for (int i = 0; i < tyDataNo.Count; i++)
{
objExcel.Cells[row, col + 2] = tyDataNo[i];
row++;
}
#endregion
//柱狀圖
Excel.Chart xlChart2 = (Excel.Chart)objWorkbook.Charts.Add(Type.Missing, objsheet, Type.Missing, Type.Missing);
Excel.Range cellRange = objsheet.get_Range((Excel.Range)objsheet.Cells[2, 6], (Excel.Range)objsheet.Cells[3 + txDataColumn.Count - 1, 8]);
//1-cellRange:數據源的範圍,2-圖表類型,3-Type.Missing,4-在圖表上將列或行用作數據系列的方式,5、6-第五個第六個參數設置圖表的x軸和y軸分別是數據源的哪些列/行,7-圖表是否有圖例,8、9、10-設置標題
xlChart2.ChartWizard(cellRange,
Excel.XlChartType.xlColumnStacked, Type.Missing,
Excel.XlRowCol.xlColumns, 1, 1, true,
"管理人員校準情況", "用戶名", "校準個數",
"");
xlChart2.Location(Excel.XlChartLocation.xlLocationAsObject, objsheet.Name);
Excel.Range oResizeRange1 = (Excel.Range)objsheet.Rows.get_Item(1);
Excel.Range oResizeRange2 = (Excel.Range)objsheet.Columns.get_Item(10);
objsheet.Shapes.Item("Chart 2").Top = oResizeRange1.Top; //調圖表的位置上邊距--1行的高度
objsheet.Shapes.Item("Chart 2").Left = (float)(double)oResizeRange2.Left;//調圖表的位置左邊距--10列的寬度
objsheet.Shapes.Item("Chart 2").Width = 300; //調圖表的寬度
objsheet.Shapes.Item("Chart 2").Height = 200; //調圖表的高度
//保存文件
objWorkbook.SaveAs(fileNameString, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
}
catch (Exception error)
{
MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
finally
{
//關閉Excel應用
if (objWorkbook != null) objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
if (objExcel.Workbooks != null) objExcel.Workbooks.Close();
if (objExcel != null) objExcel.Quit();
objsheet = null;
objWorkbook = null;
objExcel = null;
}
}
}
}