做ASPNET項目都會碰到ASPNET報表問題中國式報表的繁瑣讓我很是反感可沒法子啊客戶有需求!自然而然想用DCOM直接操作EXCEL馬馬虎虎算是成了雖說安全性不好資源浪費嚴重畢竟在局域網中使用湊合吧
不過總是討厭在服務端那個沒有結束的EXCEL進程……一定要把敵人殺死!google了一把原來當今中外都有這個ASPNET報表問題解決方案也有Windows XP下搞定但我在WINDOWS 下搞了整整一天也沒殺死得真想自個殺了後來有人推薦了個控件ASPOSE解決ASPNET報表問題這玩意強服務端都不用裝EXCEL效果不錯!想偷懶的用這玩意一個字——爽!
private string subTotal(int row int num)
{
string r = (row+)ToString();
if( num == )//
{
return =C+r++F+r+I+r;
}
else
return =E+r++H+r+K+r;
}
private string sumTotal(string scolint firstrowint num){
string r = numToString();
string fr = firstrowToString();
return =Sum(+scol+fr+:+scol+r+);
}
private void CreateExcelWorkbook(DataRow[] rows)
{
string strCurrentDir = ServerMapPath() + \\TempReports\\;
string licenseFile = MapPath() + \\XML\\AsposeExcellic;
Excel excel = new Excel(licenseFile this);
string designerFile = strCurrentDir+cangkuxls;
excelOpen(designerFile);
Worksheet sheet = excelWorksheets[Sheet];
sheetName = wuzi;
Cells cells = sheetCells;
int styleIndex;
styleIndex = excelStylesAdd();
AsposeExcelStyle stylecell = excelStyles[styleIndex];
stylecellBorders[BorderTypeLeftBorder]LineStyle=CellBorderTypeThin;
stylecellBorders[BorderTypeTopBorder]LineStyle = CellBorderTypeThin;
stylecellBorders[BorderTypeRightBorder]LineStyle=CellBorderTypeThin;
stylecellBorders[BorderTypeBottomBorder]LineStyle = CellBorderTypeThin;
int iRow =;
foreach(DataRow row in rows)
{
cells[iRow]PutValue(row[]ToString());
cells[iRow]PutValue(row[]ToString());
cells[iRow]PutValue(row[]);
cells[iRow]PutValue( row[]);
cells[iRow]PutValue(row[]);
cells[iRow]PutValue(row[]);
cells[iRow]PutValue(row[]);
cells[iRow]PutValue(row[]);
cells[iRow]PutValue(row[]);
cells[iRow]PutValue(row[]);
cells[iRow]PutValue(row[]);
cells[iRow]Formula=subTotal(iRow);
cells[iRow]PutValue(row[]);
cells[iRow]Formula=subTotal(iRow);
iRow++;
}
string zj=總計;
cells[iRow]PutValue(zj);
cells[iRow]PutValue(zj);
cells[iRow]PutValue(zj);
cells[iRow]PutValue(zj);
cells[iRow]Formula=sumTotal(EiRow);
cells[iRow]Formula=sumTotal(HiRow);
cells[iRow]Formula=sumTotal(KiRow);
cells[iRow]Formula=sumTotal(NiRow);
cells[]PutValue(cpStartDateSelectedDateToShortDateString());
cells[]PutValue(cpEndDateSelectedDateToShortDateString());
Range range = cellsCreateRange(iRow+);
rangeStyle = stylecell;
for(int i = ; i < excelWorksheetsCount ; i ++)
{
sheet = excelWorksheets[i];
if(sheetName != wuzi)
{
excelWorksheetsRemoveAt(i);
i ;
}
}
/*ResponseClear();
ResponseBuffer= true;
ResponseCharset=GB;
thisResponseContentEncoding=SystemTextEncodingGetEncoding(GB);*/
excelSave(HttpUtilityUrlEncode(物資進出庫匯總xlsEncodingUTF) SaveTypeOpenInBrowserFileFormatTypeDefaultthisResponse);
}
From:http://tw.wingwit.com/Article/program/net/201311/12607.html