public static string ExportToProvit(System.Web.UI.Page objPage, string[] Pvalues) { // Pvalues[0]=行區域數據,Pvalues[1]=列區域數據,Pvalues[2]=數據區域數據 Microsoft.Office.Interop.Excel.Application m_objExcelApp; Microsoft.Office.Interop.Excel.Workbook m_objExcelWorkBook; Microsoft.Office.Interop.Excel.Worksheet m_objExcelWorkSheet; Microsoft.Office.Interop.Excel.Worksheet m_objExcelWorkSheet2; string strAbsolutePath = clsCommon.GetUploadFilePath(objPage, clsCommon.genmUploadFileKind.Templete, ""); string strRelativePath = clsCommon.GetUploadFileUrl(objPage, clsCommon.genmUploadFileKind.Templete, ""); string strFileName = Pvalues[3].ToString(); if (strFileName != null) { strFileName = strFileName.Split('/')[strFileName.Split('/').Length - 1]; } m_objExcelApp = new Microsoft.Office.Interop.Excel.Application(); m_objExcelApp.DisplayAlerts = false; m_objExcelWorkBook = m_objExcelApp.Workbooks.Open(strAbsolutePath + strFileName, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); m_objExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objExcelWorkBook.Sheets["每日一報透視表"]; DataTable dt = (DataTable)HttpContext.Current.Session["PVDT"]; int row = 1; int col = 1; if (dt != null) { row = dt.Rows.Count+1; col = dt.Columns.Count; HttpContext.Current.Session["PVDT"] = null;//mod by chairuirui 2013-1-21 } Microsoft.Office.Interop.Excel.PivotCaches objPivot = m_objExcelWorkBook.PivotCaches(); objPivot.Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase, "總數據!R1C1:R" + row + "C" + col + "").CreatePivotTable (m_objExcelWorkSheet.Cells[3, 1], "透視表", Type.Missing, Type.Missing);//"總數據!R2C1:R52C30" 原數據范圍SHEET名稱!R起始行C起始列:R數據總行數C數據總列數 Microsoft.Office.Interop.Excel.Range objRange = (Microsoft.Office.Interop.Excel.Range)m_objExcelWorkSheet.Cells[3, 1]; objRange.Select(); Microsoft.Office.Interop.Excel.PivotTable objTable = (Microsoft.Office.Interop.Excel.PivotTable)m_objExcelWorkSheet.PivotTables("透視表"); //數據存放的透視表 if (Pvalues[0] != "") { string[] pvX=Pvalues[0].Split('@'); for (int i = 1; i <= pvX.Length; i++) { Microsoft.Office.Interop.Excel.PivotField objField = (Microsoft.Office.Interop.Excel.PivotField)objTable.PivotFields(pvX[i-1].ToString()); //賦值行數據 objField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlRowField; objField.Position = "" + i + ""; //表示同為行數據,不同的行數據的順序 } } if (Pvalues[1] != "") { string[] pvY = Pvalues[1].Split('@'); for (int i = 1; i <= pvY.Length; i++) { Microsoft.Office.Interop.Excel.PivotField objFieldY = (Microsoft.Office.Interop.Excel.PivotField)objTable.PivotFields(pvY[i-1]); //賦值列數據 objFieldY.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField; objFieldY.Position = ""+i+""; } } if (Pvalues[2] != "") { string[] pvZ = Pvalues[2].Split('@'); for (int i = 1; i <= pvZ.Length; i++) { Microsoft.Office.Interop.Excel.PivotField objFieldN = (Microsoft.Office.Interop.Excel.PivotField)objTable.PivotFields(pvZ[i-1].ToString()); //賦值數據區域數據 objFieldN.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlDataField; objFieldN.Position = ""+i+""; } } m_objExcelWorkBook.SaveCopyAs(strAbsolutePath + strFileName); m_objExcelApp.DisplayAlerts = false; m_objExcelApp.Workbooks.Close(); m_objExcelApp.Quit(); return strRelativePath + strFileName; }