try             {

string OutFileName = "北京市国控企业污染源废气在线比对监测数据审核表" + DateTime.Now.ToString("yyyy-MM-dd");

string templateName = "online_gas_template.xml";

if (OutFileName.Contains("xls"))

{

OutFileName = OutFileName.Substring(0, OutFileName.LastIndexOf('.'));

}

int mergeColumnCount = 10;

string strSql = string.Empty;

DataTable dsData = dalReport.GetExportData(this.txtEnterName.Text, PollutionName.Text, ddl_Year.SelectedValue, ddl_Quarter.SelectedValue, ddl_Area.Items[ddl_Area.SelectedIndex].Text, ddl_Result.SelectedValue, radio_Used.SelectedValue);                 if (null == dsData) return;

int intColCount = dsData.Columns.Count;

int intRowCount = dsData.Rows.Count;

DataColumnCollection colName = dsData.Columns;

//构造EXCLE字符串                 string newFilePath = this.Server.MapPath("..") + "\\temp\\" + OutFileName + ".xls";                 System.IO.StreamWriter sw = new System.IO.StreamWriter(newFilePath);

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();

//申明两个数组,初始为0

int[] mergeDownArr = new int[mergeColumnCount];//用来保存要向下合并的行数,运行到下一行时递减

int[] mergeDownTotalArr = new int[mergeColumnCount];//记录一下合并行总数

for (int rI = 0; rI < intRowCount; rI++)//循环输出行                 {                     sbContent.Append("<Row>");                     for (int mI = 0; mI < mergeColumnCount; mI++)//循环有合并情况的列                     {                         if (mergeDownArr[mI] <= 0)                         {                             for (int n = rI + 1; n < intRowCount; n++)                             {                                 if (dsData.Rows[n][mI].ToString() == dsData.Rows[rI][mI].ToString())                                 {                                     if (mI != 1)//如果不是企业名称列,其它列是否合并要根据企业名称是否相等来判断                                     {                                         if (dsData.Rows[n][1].ToString() == dsData.Rows[rI][1].ToString())                                         {                                             mergeDownArr[mI]++;                                         }                                     }                                     else//如果是企业名称列,则直接加                                     {                                         mergeDownArr[mI]++;                                     }                                 }                                 else                                 {                                     break;                                 }                             }                             mergeDownTotalArr[mI] = mergeDownArr[mI];                         }                         else                         {                             mergeDownArr[mI]--;                         }                     }                     //要对数据的前mergeColumnCount个列进行合并相关处理                     for (int cI = 0; cI < mergeColumnCount; cI++)                     {                         int cellIndex = cI + 1;                         if (mergeDownArr[cI] == mergeDownTotalArr[cI] && mergeDownTotalArr[cI] > 0)//要输出的具有合并标志的第一行                         {                             sbContent.Append("<Cell ss:Index=\"" + cellIndex + "\" ss:MergeDown=\"" + mergeDownTotalArr[cI] + "\" ss:StyleID=\"s21\"><Data ss:Type=\"String\">" + dsData.Rows[rI][cI].ToString().Replace("<", "&lt;") + "</Data></Cell>");                         }                         else if (mergeDownTotalArr[cI] == 0)                         {

sbContent.Append("<Cell ss:Index=\"" + cellIndex + "\" ss:StyleID=\"s21\"><Data ss:Type=\"String\">" + dsData.Rows[rI][cI].ToString().Replace("<", "&lt;") + "</Data></Cell>");                         }                     }                     //处理剩下不需要合并的列                     string ssIndex = string.Empty;                     if (mergeColumnCount > 0)                     {                         ssIndex = "ss:Index=\"" + (mergeColumnCount + 1).ToString() + "\"";                     }                     for (int remainC = mergeColumnCount; remainC < intColCount; remainC++)//循环输出剩下不需要合并的列                     {                         if (remainC == mergeColumnCount)                         {                             sbContent.Append("<Cell ss:StyleID=\"s21\" " + ssIndex + " ><Data ss:Type=\"String\">" + dsData.Rows[rI][remainC].ToString().Replace("<", "&lt;") + "</Data></Cell>");                         }                         else                         {                             string txt = "s21";                             if (remainC == 16)                             {                                 if (dsData.Rows[rI][remainC].ToString().Replace("<", "&lt;") == "否")                                     txt = "s210";                             }                             sbContent.Append("<Cell ss:StyleID=\"" + txt + "\"><Data ss:Type=\"String\">" + dsData.Rows[rI][remainC].ToString().Replace("<", "&lt;") + "</Data></Cell>");                         }

}                     sbContent.Append("</Row>");                 }                 tempStr = tempStr.Replace("{Content}", sbContent.ToString());                 sw.Write(tempStr);                 sw.Close();                 ExecJs("window.open('../temp/" + OutFileName + ".xls');", Page);             }             catch (System.Exception E)             {             }

模板文件内容

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Version>14.00</Version>
 </DocumentProperties>
 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  <AllowPNG/>
 </OfficeDocumentSettings>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>10005</WindowHeight>
  <WindowWidth>10005</WindowWidth>
  <WindowTopX>120</WindowTopX>
  <WindowTopY>135</WindowTopY>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Center"/>
   <Borders/>
   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s62" ss:Name="常规 2">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Arial" x:Family="Swiss"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="m45960948" ss:Parent="s62">
   <Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="9"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s63">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
  </Style>
  <Style ss:ID="s65">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="16" ss:Color="#000000"
    ss:Bold="1"/>
  </Style>
  <Style ss:ID="s68" ss:Parent="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="9" ss:Color="#000000"
    ss:Bold="1"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s69" ss:Parent="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="Tahoma" x:CharSet="134" x:Family="Swiss" ss:Size="9"
    ss:Color="#000000" ss:Bold="1"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s70" ss:Parent="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="9" ss:Color="#000000"
    ss:Bold="1"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s71" ss:Parent="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="Tahoma" x:CharSet="134" x:Family="Swiss" ss:Size="9"
    ss:Color="#000000" ss:Bold="1"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
   <!--通用样式s21,程序里写入行数据时使用此样式,不可少-->
   <Style ss:ID="s21">
     <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
     <Borders>
       <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
       <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
       <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
       <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
     </Borders>
   </Style>
 </Styles>
 <Worksheet ss:Name="污水处理厂在线比对监测数据审核表">
  <Table ss:ExpandedColumnCount="18" x:FullColumns="1"
   x:FullRows="1" ss:StyleID="s63" ss:DefaultColumnWidth="54"
   ss:DefaultRowHeight="13.5">
   <Column ss:Index="6" ss:StyleID="s63" ss:AutoFitWidth="0" ss:Width="69.75"/>
   <Row ss:AutoFitHeight="0" ss:Height="42.5625">
    <Cell ss:MergeAcross="17" ss:StyleID="s65"><Data ss:Type="String">北京市国控企业污染源污水处理厂在线比对监测数据审核表</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0" ss:Height="24">
    <Cell ss:StyleID="s68"><Data ss:Type="String">区县名称</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">企业名称</Data></Cell>
    <Cell ss:StyleID="s69"><Data ss:Type="String">监测点名称</Data></Cell>
    <Cell ss:StyleID="s68"><Data ss:Type="String">自动监测系统安装位置</Data></Cell>
    <Cell ss:StyleID="s70"><Data ss:Type="String">监测原理</Data></Cell>
    <Cell ss:StyleID="s70"><Data ss:Type="String">自动监测设备型号</Data></Cell>
    <Cell ss:StyleID="s70"><Data ss:Type="String">自动监测设备生产厂家</Data></Cell>
     <Cell ss:StyleID="s70">
       <Data ss:Type="String">比对监测日期</Data>
     </Cell>
     <Cell ss:StyleID="s71">
       <Data ss:Type="String">监测因子</Data>
     </Cell>
     <Cell ss:StyleID="s71">
       <Data ss:Type="String">数据类型</Data>
     </Cell>
    <Cell ss:StyleID="s71"><Data ss:Type="String">手工监测数据</Data></Cell>
    <Cell ss:StyleID="s71"><Data ss:Type="String">自动监测数据</Data></Cell>
    <Cell ss:StyleID="s70"><Data ss:Type="String">单位</Data></Cell>
    <Cell ss:StyleID="s70"><Data ss:Type="String">比对结果</Data></Cell>
    <Cell ss:StyleID="s70"><Data ss:Type="String">评判标准</Data></Cell>
     <Cell ss:StyleID="s70">
       <Data ss:Type="String">是否合格</Data>
     </Cell>
    <Cell ss:StyleID="s70"><Data ss:Type="String">监测项目是否合格</Data></Cell>
    <Cell ss:StyleID="s70"><Data ss:Type="String">监测点位全项目是否合格</Data></Cell>
   </Row>
    {Content}
   <Row ss:AutoFitHeight="0">
    <Cell ss:MergeAcross="17" ss:StyleID="m45960948"><Data ss:Type="String">签发:                                      审核:                                             录入:                          第   页/共   页                          </Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <Unsynced/>
   <Print>
    <ValidPrinterInfo/>
    <PaperSizeIndex>9</PaperSizeIndex>
    <HorizontalResolution>200</HorizontalResolution>
    <VerticalResolution>200</VerticalResolution>
   </Print>
   <Selected/>
   <Panes>
    <Pane>
     <Number>3</Number>
     <ActiveRow>2</ActiveRow>
     <RangeSelection>R3C1:R3C18</RangeSelection>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>

读取excel模板填充数据 并合并相同文本单元格的更多相关文章

  1. POI3.10读取Excel模板填充数据后生成新的Excel文件

    private final DecimalFormat df = new DecimalFormat("#0.00"); public void test(){ String fi ...

  2. 用NPOI从DataTable到Excel,向Excel模板填充数据

    DataTable---->Excel,填充数据 private IWorkbook workbook = null; private ISheet sheet = null; private ...

  3. 根据EXCEL模板填充数据

    string OutFileName = typeName+"重点源达标率" + DateTime.Now.ToString("yyyy-MM-dd");    ...

  4. poi读取excel模板,填充内容并导出,支持导出2007支持公式自动计算

    /** * 版权所有(C) 2016 * @author www.xiongge.club * @date 2016-12-7 上午10:03:29 */ package xlsx; /** * @C ...

  5. java后台读取excel模板数据

    /** * 读取EXCEL模板数据 * * @param excelFilePath excel文件路径 * @param dataRowNum 开始读取数据的行数 * @param keyRowNu ...

  6. Aspose.Cells 首次使用,用到模版填充数据,合并单元格,换行

    Aspose.Cells 首次使用,用到模版填充数据,合并单元格,换行 模版格式,图格式是最简单的格式,但实际效果不是这种,实际效果图如图2 图2 ,注意看红色部分,一对一是正常的,但是有一对多的订单 ...

  7. sql 读取excel中的数据

    select 列名 as 字段名 from openBowSet('MSDASQL.1','driver=Microsoft Excel Driver(*.xls);dbq=文件存放地址','sele ...

  8. C#读取Excel表格的数据

    1.创建工程后,需要下载 EPPlus.dll 添加到工程中,这里有一个下载地址:https://download.csdn.net/download/myunity/10784634 2.下面仅实现 ...

  9. SpringBoot(十三)_springboot上传Excel并读取excel中的数据

    今天工作中,发现同事在整理数据,通过excel上传到数据库.所以现在写了篇利用springboot读取excel中的数据的demo.至于数据的进一步处理,大家肯定有不同的应用场景,自行修改 pom文件 ...

随机推荐

  1. poj 1161 Floyd+枚举

    题意是: 给出n个点,围成m个区域.从区域到另一个区域间需穿过至少一条边(若两区域相邻)——边连接着两点. 给出这么一幅图,并给出一些点,问从这些点到同一个区域的穿过边数最小值. 解题思路如下: 将区 ...

  2. Super超级ERP系统---(8)订单管理--订单创建

    订单管理是ERP系统中一个重要模块,客户下订单,ERP通过订单来为客户进行配送.订单模块主要包括订单创建,订单修改,订单审核,订单取消,订单分配,订单打印,订单拣货,订单出库.在随后的几节里我们看看这 ...

  3. 提示“CD/DVD找不到媒体所需的驱动”

    最近在帮我姐安装win7系统时提 示“CD/DVD找不到媒体所需的驱动”,我用的是U盘安装方式,觉得奇怪,那个镜像文件我已经安装过几十次都没有出错,显然是不会有错的.但是新买的电 脑又不会太大的问题, ...

  4. AI:**消灭程序员需要一百年吗?

    这篇博文真的很长,不过挺有意思.关于智能机器人的发展前景还是很广的,因为每一步都异常艰难,而什么时候可以终止还无法预料,所以程序员没办法失业啊! 转自于图灵社区:http://www.ituring. ...

  5. Pycharm 4.5.4 for ubuntu 16.04 下载与安装教程

    首先,我们需要有一台已经安装好 ubuntu 16.04 的操作系统,并且配置好java环境: 方法1: 默认安装 apt-get install default-jdk -y # 安装官网最新的ja ...

  6. leetCode 929 独特的电子邮件地址

    题目: 每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔. 例如,在 alice@leetcode.com中, alice 是本地名称,而 leetcode.com 是域名. 除了小写字母 ...

  7. Scalable Web Architecture and Distributed Systems

    转自:http://aosabook.org/en/distsys.html Scalable Web Architecture and Distributed Systems Kate Matsud ...

  8. WEBGL学习【十五】利用WEBGL实现三维场景的一般思路总结

    实现三维场景载入操作的实现步骤: 主要知识点:着色器,纹理贴图,文件载入 实现思路: 获取canvas,初始化WEBGL上下文信息. 主要是实现WEBGL上下文的获取,设置视的大小,此时gl存储了WE ...

  9. P1085 不高兴的津津

    ... 题目描述 津津上初中了.妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班.另外每周妈妈还会送她去学习朗诵.舞蹈和钢琴.但是津津如果一天上课超过八个小时就会不 ...

  10. 猫狗分类--Tensorflow实现

    贴一张自己画的思维导图  数据集准备 kaggle猫狗大战数据集(训练),微软的不需要FQ 12500张cat 12500张dog 生成图片路径和标签的List step1:获取D:/Study/Py ...