C#读取EXCEL模版方法

//读取EXCEL模版
              FileStream fs = new FileStream(tempName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
              HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
              ISheet sheet = hssfworkbook.GetSheetAt(0);
             
              //向Excel模版写标题
              sheet.GetRow(Convert.ToInt16(indexCaption[0])).GetCell(Convert.ToInt16(indexCaption[1])).SetCellValue(tmpReportDate);

              DataTable dt = new DataTable();
              //连接数据库
              SqlConnection sqlconn = new SqlConnection(DbProvider.ConnectionString);
              SqlCommand sqlcomm = sqlconn.CreateCommand();
              sqlconn.Open();
              sqlcomm.CommandType = CommandType.StoredProcedure;
              sqlcomm.Parameters.Add("@pmDate", SqlDbType.NVarChar).Value = curDate;
              sqlcomm.Parameters.Add("@pmProductCode", SqlDbType.NVarChar).Value = "";
              sqlcomm.Parameters.Add("@pmBranchCode", SqlDbType.NVarChar).Value = "";
              if (IndexSystemID.Length > 0) sqlcomm.Parameters.Add("@pmSystemCode", SqlDbType.NVarChar).Value = "";
              sqlcomm.Parameters.Add("@pmLX", SqlDbType.NVarChar).Value = "";

              for (int s = 0; s < storeName.Length; s++)
              {
                  sqlcomm.Parameters["@pmProductCode"].Value = indexProductID[s];
                  sqlcomm.Parameters["@pmBranchCode"].Value = indexBranchID[s];
                  if (IndexSystemID.Length > 0) sqlcomm.Parameters["@pmSystemCode"].Value = IndexSystemID[s];
                  sqlcomm.Parameters["@pmLX"].Value = pmType;
                  sqlcomm.CommandText = storeName[s];

                  dt.Load(sqlcomm.ExecuteReader());
                  if (dt.Rows.Count > 0)
                  {
                      int i = 0,j;
                      for (int r = Convert.ToInt16(indexLocation[s * 4]); r < dt.Rows.Count + Convert.ToInt16(indexLocation[s * 4]); r++, i++)
                      {
                          j = 0;
                          IRow row = sheet.GetRow(r);
                          for (int col = Convert.ToInt16(indexLocation[s * 4 + 2]); col < Convert.ToInt16(indexLocation[s * 4 + 2]) + Convert.ToInt16(indexLocation[s * 4 + 3]); col++, j++)
                              row.GetCell(col).SetCellValue(dt.Rows[i][j].ToString());
                      }
                  }
                  //将DataTable清空,如果不清空将会导致dt的数据累计
                  dt.Clear();
              }
              //把工作簿写入文件
              using (FileStream wfs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
              {
                  hssfworkbook.Write(wfs);
                  wfs.Close();
                  //downFile(fileName);
              }
              sqlconn.Close();
              hssfworkbook.Dispose();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值