C# EXCEL 透视表使用 多行多列的导出透视表
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;
}
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