string OutFileName = typeName+"重点源达标率" + DateTime.Now.ToString("yyyy-MM-dd"); string templateName = "keySources_template.xml"; DataTable dsData = dal.T_StanderEvaluateData_DefaultListPR(parameterList.ToArray()); if (null == dsData||dsData.Rows.Count<1) return; int intColCount = dsData.Columns.Count; int intRowCount = dsData.Rows.Count;
//构造EXCLE字符串 System.IO.StreamWriter sw = new System.IO.StreamWriter(this.Server.MapPath("..") + "\\temp\\" + OutFileName + ".xls"); string filePath = this.Server.MapPath("..") + "\\ExcelTemplet\\" + templateName; System.IO.FileStream tempFile = new System.IO.FileStream(filePath, System.IO.FileMode.Open);//读取模板文件
System.IO.StreamReader sr = new System.IO.StreamReader(tempFile); string tempStr = sr.ReadToEnd(); sr.Close();
//内容开始 StringBuilder sbContent = new StringBuilder(); sbContent.Append("<Worksheet ss:Name=\"" + typeName + "重点源达标率表\"><Table"); sbContent.Append(" x:FullColumns=\"1\" x:FullRows=\"1\" ss:StyleID=\"s63\" ss:DefaultColumnWidth=\"54\""); sbContent.Append(" ss:DefaultRowHeight=\"13.5\"><Row ss:AutoFitHeight=\"0\" ss:Height=\"42.5625\">"); sbContent.Append("<Cell ss:MergeAcross=\"" + (intColCount-1) + "\" ss:StyleID=\"s65\">"); sbContent.Append("<Data ss:Type=\"String\">" + typeName + "重点源达标率表</Data></Cell></Row>"); sbContent.Append("<Row ss:AutoFitHeight=\"0\" ss:Height=\"24\">"); for (int i = 0; i < dsData.Columns.Count; i++) { sbContent.Append("<Cell ss:StyleID=\"s68\"><Data ss:Type=\"String\">"+dsData.Columns[i].ColumnName+"</Data></Cell>"); } sbContent.Append("</Row>"); for (int k = 0; k < dsData.Rows.Count; k++) { sbContent.Append("<Row>"); for (int m = 0; m < dsData.Columns.Count; m++) { if("重点源数"==dsData.Columns[m].ColumnName) keyPollutionCount+=Convert.ToInt32(dsData.Rows[k][m].ToString()); if ("实测重点源数" == dsData.Columns[m].ColumnName) keyAllPollutionCount += Convert.ToInt32(dsData.Rows[k][m].ToString()); string value = dsData.Rows[k][m].ToString(); if ("0%" == value) value = ""; sbContent.Append("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">" +value +"</Data></Cell>"); } sbContent.Append("</Row>"); } sbContent.Append("<Row><Cell ss:StyleID=\"s21\" ss:MergeAcross=\"1\"><Data ss:Type=\"String\">合计</Data></Cell>"); sbContent.Append("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">"+keyPollutionCount+"</Data></Cell>"); sbContent.Append("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">" + keyAllPollutionCount + "</Data></Cell>"); DataTable dtPollutionRates = dal.GetAllPollutionRate(parameterList.ToArray()); if (null == dtPollutionRates || dtPollutionRates.Rows.Count < 0) return; for (int i = 0; i < dtPollutionRates.Rows.Count; i++) { string tempProterty = dtPollutionRates.Rows[i]["AllRate"].ToString(); if ("0%" == tempProterty) tempProterty = ""; sbContent.Append("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">" + tempProterty + "</Data></Cell>"); } sbContent.Append("</Row>"); sbContent.Append("</Table>"); tempStr = tempStr.Replace("{Content}", sbContent.ToString()); sw.Write(tempStr); sw.Close(); ClientScript.RegisterStartupScript(typeof(Page), "a", "<script language=\"Javascript\" type=\"text/javascript\">window.open('../temp/" + OutFileName + ".xls');</script>"); BindGrid(); } catch (Exception ex) { throw; }
<?xml version="1.0"?>
<?mso-application prog/>
</PageSetup>
<Unsynced/>
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<HorizontalResolution>200</HorizontalResolution>
<VerticalResolution>200</VerticalResolution>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>1</ActiveRow>
<ActiveCol>9</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>