ExcelHelper(Excel和C#、asp.net导入导出,通用类)(二)

http://blog.163.com/it_software/blog/static/1238582962010518113452975/


/// <summary>
  /// 将DataTable数据写入Excel文件(不分页)
  /// </summary>
  /// <param name="dt">DataTable</param>
  /// <param name="top">表格数据起始行索引</param>
  /// <param name="left">表格数据起始列索引</param>
  public void DataTableToExcel(DataTable dt,int top,int left)
  {
   int rowCount = dt.Rows.Count;  //DataTable行数
   int colCount = dt.Columns.Count; //DataTable列数

   //利用二维数组批量写入
   string[,] arr = new string[rowCount,colCount];

   for(int j=0;j<rowCount;j++)
   {
    for(int k=0;k<colCount;k++)
    {
     arr[j,k] = dt.Rows[j][k].ToString();
    }
   }

   range = (Excel.Range)workSheet.Cells[top,left];
   range = range.get_Resize(rowCount,colCount);
   range.Value = arr;
  }


  /// <summary>
  /// 将DataTable数据写入Excel文件(自动分页,并指定要合并的列索引)
  /// </summary>
  /// <param name="dt">DataTable</param>
  /// <param name="rows">每个WorkSheet写入多少行数据</param>
  /// <param name="top">表格数据起始行索引</param>
  /// <param name="left">表格数据起始列索引</param>
  /// <param name="mergeColumnIndex">DataTable中要合并相同行的列索引,从0开始</param>
  public void DataTableToExcel(DataTable dt,int rows,int top,int left,int mergeColumnIndex)
  {
   int rowCount = dt.Rows.Count;  //源DataTable行数
   int colCount = dt.Columns.Count; //源DataTable列数
   sheetCount = this.GetSheetCount(rowCount,rows); //WorkSheet个数
//   StringBuilder sb;
    
   //复制sheetCount-1个WorkSheet对象
   for(int i=1;i<sheetCount;i++)
   {
    workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
    workSheet.Copy(missing,workBook.Worksheets[i]);
   }
  
   for(int i=1;i<=sheetCount;i++)
   {
    int startRow = (i - 1) * rows;  //记录起始行索引
    int endRow = i * rows;   //记录结束行索引
  
    //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
    if(i == sheetCount)
     endRow = rowCount;
  
    //获取要写入数据的WorkSheet对象,并重命名
    workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
    workSheet.Name = sheetPrefixName + "-" + i.ToString();
  
    //将dt中的数据写入WorkSheet
//    for(int j=0;j<endRow-startRow;j++)
//    {
//     for(int k=0;k<colCount;k++)
//     {
//      workSheet.Cells[top + j,left + k] = dt.Rows[startRow + j][k].ToString();
//     }
//    }

    //利用二维数组批量写入
    int row = endRow-startRow;
    string[,] ss = new string[row,colCount];

    for(int j=0;j<row;j++)
    {
     for(int k=0;k<colCount;k++)
     {
      ss[j,k] = dt.Rows[startRow + j][k].ToString();
     }
    }

    range = (Excel.Range)workSheet.Cells[top,left];
    range = range.get_Resize(row,colCount);
    range.Value = ss;

    //合并相同行
    this.MergeRows(workSheet,left+mergeColumnIndex,top,rows);
      
   }
  }


  /// <summary>
  /// 将二维数组数据写入Excel文件(自动分页)
  /// </summary>
  /// <param name="arr">二维数组</param>
  /// <param name="rows">每个WorkSheet写入多少行数据</param>
  /// <param name="top">行索引</param>
  /// <param name="left">列索引</param>
  public void ArrayToExcel(string[,] arr,int rows,int top,int left)
  {
   int rowCount = arr.GetLength(0);  //二维数组行数(一维长度)
   int colCount = arr.GetLength(1); //二维数据列数(二维长度)
   sheetCount = this.GetSheetCount(rowCount,rows); //WorkSheet个数

   //复制sheetCount-1个WorkSheet对象
   for(int i=1;i<sheetCount;i++)
   {
    workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
    workSheet.Copy(missing,workBook.Worksheets[i]);
   }

   //将二维数组数据写入Excel
   for(int i=sheetCount;i>=1;i--)
   {
    int startRow = (i - 1) * rows;  //记录起始行索引
    int endRow = i * rows;   //记录结束行索引

    //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
    if(i == sheetCount)
     endRow = rowCount;

    //获取要写入数据的WorkSheet对象,并重命名
    workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
    workSheet.Name = sheetPrefixName + "-" + i.ToString();

    //将二维数组中的数据写入WorkSheet
//    for(int j=0;j<endRow-startRow;j++)
//    {
//     for(int k=0;k<colCount;k++)
//     {
//      workSheet.Cells[top + j,left + k] = arr[startRow + j,k];
//     }
//    }

    //利用二维数组批量写入
    int row = endRow-startRow;
    string[,] ss = new string[row,colCount];

    for(int j=0;j<row;j++)
    {
     for(int k=0;k<colCount;k++)
     {
      ss[j,k] = arr[startRow + j,k];
     }
    }

    range = (Excel.Range)workSheet.Cells[top,left];
    range = range.get_Resize(row,colCount);
    range.Value = ss;
   }
   
  }//end ArrayToExcel


  /// <summary>
  /// 将二维数组数据写入Excel文件(不分页)
  /// </summary>
  /// <param name="arr">二维数组</param>
  /// <param name="top">行索引</param>
  /// <param name="left">列索引</param>
  public void ArrayToExcel(string[,] arr,int top,int left)
  {
   int rowCount = arr.GetLength(0);  //二维数组行数(一维长度)
   int colCount = arr.GetLength(1); //二维数据列数(二维长度)

   range = (Excel.Range)workSheet.Cells[top,left];
   range = range.get_Resize(rowCount,colCount);
   range.FormulaArray = arr;
   
  }//end ArrayToExcel

  /// <summary>
  /// 将二维数组数据写入Excel文件(不分页)
  /// </summary>
  /// <param name="arr">二维数组</param>
  /// <param name="top">行索引</param>
  /// <param name="left">列索引</param>
  /// <param name="isFormula">填充的数据是否需要计算</param>
  public void ArrayToExcel(string[,] arr,int top,int left,bool isFormula)
  {
   int rowCount = arr.GetLength(0);  //二维数组行数(一维长度)
   int colCount = arr.GetLength(1); //二维数据列数(二维长度)

   range = (Excel.Range)workSheet.Cells[top,left];
   range = range.get_Resize(rowCount,colCount);

   //注意:使用range.FormulaArray写合并的单元格会出问题
   if(isFormula)
    range.FormulaArray = arr;
   else
    range.Value = arr;
   
  }//end ArrayToExcel

  /// <summary>
  /// 将二维数组数据写入Excel文件(不分页),合并指定列的相同行
  /// </summary>
  /// <param name="arr">二维数组</param>
  /// <param name="top">行索引</param>
  /// <param name="left">列索引</param>
  /// <param name="isFormula">填充的数据是否需要计算</param>
  /// <param name="mergeColumnIndex">需要合并行的列索引</param>
  public void ArrayToExcel(string[,] arr,int top,int left,bool isFormula,int mergeColumnIndex)
  {
   int rowCount = arr.GetLength(0);  //二维数组行数(一维长度)
   int colCount = arr.GetLength(1); //二维数据列数(二维长度)

   range = (Excel.Range)workSheet.Cells[top,left];
   range = range.get_Resize(rowCount,colCount);

   //注意:使用range.FormulaArray写合并的单元格会出问题
   if(isFormula)
    range.FormulaArray = arr;
   else
    range.Value = arr;

   this.MergeRows(workSheet,mergeColumnIndex,top,rowCount);
   
  }//end ArrayToExcel

  /// <summary>
  /// 将二维数组数据写入Excel文件(不分页)
  /// </summary>
  /// <param name="sheetIndex">工作表索引</param>
  /// <param name="arr">二维数组</param>
  /// <param name="top">行索引</param>
  /// <param name="left">列索引</param>
  public void ArrayToExcel(int sheetIndex,string[,] arr,int top,int left)
  {
   if(sheetIndex > this.WorkSheetCount)
   {
    this.KillExcelProcess();
    throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
   }

   // 改变当前工作表
   this.workSheet = (Excel.Worksheet)this.workBook.Sheets.get_Item(sheetIndex);

   int rowCount = arr.GetLength(0);  //二维数组行数(一维长度)
   int colCount = arr.GetLength(1); //二维数据列数(二维长度)

   range = (Excel.Range)workSheet.Cells[top,left];
   range = range.get_Resize(rowCount,colCount);

   range.Value2 = arr;
   
  }//end ArrayToExcel

  /// <summary>
  /// 将二维数组数据写入Excel文件(自动分页,并指定要合并的列索引)
  /// </summary>
  /// <param name="arr">二维数组</param>
  /// <param name="rows">每个WorkSheet写入多少行数据</param>
  /// <param name="top">行索引</param>
  /// <param name="left">列索引</param>
  /// <param name="mergeColumnIndex">数组的二维索引,相当于DataTable的列索引,索引从0开始</param>
  public void ArrayToExcel(string[,] arr,int rows,int top,int left,int mergeColumnIndex)
  {
   int rowCount = arr.GetLength(0);  //二维数组行数(一维长度)
   int colCount = arr.GetLength(1); //二维数据列数(二维长度)
   sheetCount = this.GetSheetCount(rowCount,rows); //WorkSheet个数

   //复制sheetCount-1个WorkSheet对象
   for(int i=1;i<sheetCount;i++)
   {
    workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
    workSheet.Copy(missing,workBook.Worksheets[i]);
   }

   //将二维数组数据写入Excel
   for(int i=sheetCount;i>=1;i--)
   {
    int startRow = (i - 1) * rows;  //记录起始行索引
    int endRow = i * rows;   //记录结束行索引

    //若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
    if(i == sheetCount)
     endRow = rowCount;

    //获取要写入数据的WorkSheet对象,并重命名
    workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
    workSheet.Name = sheetPrefixName + "-" + i.ToString();

    //将二维数组中的数据写入WorkSheet
    for(int j=0;j<endRow-startRow;j++)
    {
     for(int k=0;k<colCount;k++)
     {
      workSheet.Cells[top + j,left + k] = arr[startRow + j,k];
     }
    }

    //利用二维数组批量写入
    int row = endRow-startRow;
    string[,] ss = new string[row,colCount];

    for(int j=0;j<row;j++)
    {
     for(int k=0;k<colCount;k++)
     {
      ss[j,k] = arr[startRow + j,k];
     }
    }

    range = (Excel.Range)workSheet.Cells[top,left];
    range = range.get_Resize(row,colCount);
    range.Value = ss;

    //合并相同行
    this.MergeRows(workSheet,left+mergeColumnIndex,top,rows);
   }
   
  }//end ArrayToExcel
  #endregion

  #region WorkSheet Methods

  /// <summary>
  /// 改变当前工作表
  /// </summary>
  /// <param name="sheetIndex">工作表索引</param>
  public void ChangeCurrentWorkSheet(int sheetIndex)
  {
   //若指定工作表索引超出范围,则不改变当前工作表
   if(sheetIndex < 1)
    return;

   if(sheetIndex > this.WorkSheetCount)
    return;

   this.workSheet = (Excel.Worksheet)this.workBook.Sheets.get_Item(sheetIndex);
  }
  /// <summary>
  /// 隐藏指定名称的工作表
  /// </summary>
  /// <param name="sheetName">工作表名称</param>
  public void HiddenWorkSheet(string sheetName)
  {
   try
   {
    Excel.Worksheet sheet = null;

    for(int i=1;i<=this.WorkSheetCount;i++)
    {
     workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
     
     if(workSheet.Name == sheetName)
      sheet = workSheet;
    }

    if(sheet != null)
     sheet.Visible = Excel.XlSheetVisibility.xlSheetHidden;
    else
    {
     this.KillExcelProcess();
     throw new Exception("名称为\"" + sheetName + "\"的工作表不存在");
    }
   }
   catch(Exception e)
   {
    this.KillExcelProcess();
    throw e;
   }
  }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值