1.先用了NPOI,去做,HSSFWorkbook 里面有一个Copy方法,但这个只支持office2003。

对应的XSSFWorkbook没有些方法。

而且这个这个方法对devexpress导出的2003的excel文件读取不了,出现异常,需要用excel打开后,另存一下才行。

var fs = new FileStream("c://pivotGrid.xls", FileMode.Open, FileAccess.Read);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
var sheet = workbook.GetSheetAt() as HSSFSheet; var fs2 = new FileStream("c://test3.xls", FileMode.Create);
var workbook2 = new HSSFWorkbook();
sheet.CopyTo(workbook2, "a", true, true); workbook2.Write(fs2);
fs2.Close();

网上有些人对POI写过类似的方法:

http://blog.csdn.net/wutbiao/article/details/8696446

public class POIUtils {
// /**
// * 把一个excel中的cellstyletable复制到另一个excel,这里会报错,不能用这种方法,不明白呀?????
// * @param fromBook
// * @param toBook
// */
// public static void copyBookCellStyle(HSSFWorkbook fromBook,HSSFWorkbook toBook){
// for(short i=0;i<fromBook.getNumCellStyles();i++){
// HSSFCellStyle fromStyle=fromBook.getCellStyleAt(i);
// HSSFCellStyle toStyle=toBook.getCellStyleAt(i);
// if(toStyle==null){
// toStyle=toBook.createCellStyle();
// }
// copyCellStyle(fromStyle,toStyle);
// }
// }
/**
* 复制一个单元格样式到目的单元格样式
* @param fromStyle
* @param toStyle
*/
public static void copyCellStyle(HSSFCellStyle fromStyle,
HSSFCellStyle toStyle) {
toStyle.setAlignment(fromStyle.getAlignment());
//边框和边框颜色
toStyle.setBorderBottom(fromStyle.getBorderBottom());
toStyle.setBorderLeft(fromStyle.getBorderLeft());
toStyle.setBorderRight(fromStyle.getBorderRight());
toStyle.setBorderTop(fromStyle.getBorderTop());
toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); //背景和前景
toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); toStyle.setDataFormat(fromStyle.getDataFormat());
toStyle.setFillPattern(fromStyle.getFillPattern());
// toStyle.setFont(fromStyle.getFont(null));
toStyle.setHidden(fromStyle.getHidden());
toStyle.setIndention(fromStyle.getIndention());//首行缩进
toStyle.setLocked(fromStyle.getLocked());
toStyle.setRotation(fromStyle.getRotation());//旋转
toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
toStyle.setWrapText(fromStyle.getWrapText()); }
/**
* Sheet复制
* @param fromSheet
* @param toSheet
* @param copyValueFlag
*/
public static void copySheet(HSSFWorkbook wb,HSSFSheet fromSheet, HSSFSheet toSheet,
boolean copyValueFlag) {
//合并区域处理
mergerRegion(fromSheet, toSheet);
for (Iterator rowIt = fromSheet.rowIterator(); rowIt.hasNext();) {
HSSFRow tmpRow = (HSSFRow) rowIt.next();
HSSFRow newRow = toSheet.createRow(tmpRow.getRowNum());
//行复制
copyRow(wb,tmpRow,newRow,copyValueFlag);
}
}
/**
* 行复制功能
* @param fromRow
* @param toRow
*/
public static void copyRow(HSSFWorkbook wb,HSSFRow fromRow,HSSFRow toRow,boolean copyValueFlag){
for (Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext();) {
HSSFCell tmpCell = (HSSFCell) cellIt.next();
HSSFCell newCell = toRow.createCell(tmpCell.getCellNum());
copyCell(wb,tmpCell, newCell, copyValueFlag);
}
}
/**
* 复制原有sheet的合并单元格到新创建的sheet
*
* @param sheetCreat 新创建sheet
* @param sheet 原有的sheet
*/
public static void mergerRegion(HSSFSheet fromSheet, HSSFSheet toSheet) {
int sheetMergerCount = fromSheet.getNumMergedRegions();
for (int i = ; i < sheetMergerCount; i++) {
Region mergedRegionAt = fromSheet.getMergedRegionAt(i);
toSheet.addMergedRegion(mergedRegionAt);
}
}
/**
* 复制单元格
*
* @param srcCell
* @param distCell
* @param copyValueFlag
* true则连同cell的内容一起复制
*/
public static void copyCell(HSSFWorkbook wb,HSSFCell srcCell, HSSFCell distCell,
boolean copyValueFlag) {
HSSFCellStyle newstyle=wb.createCellStyle();
copyCellStyle(srcCell.getCellStyle(), newstyle);
distCell.setEncoding(srcCell.getEncoding());
//样式
distCell.setCellStyle(newstyle);
//评论
if (srcCell.getCellComment() != null) {
distCell.setCellComment(srcCell.getCellComment());
}
// 不同数据类型处理
int srcCellType = srcCell.getCellType();
distCell.setCellType(srcCellType);
if (copyValueFlag) {
if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(srcCell)) {
distCell.setCellValue(srcCell.getDateCellValue());
} else {
distCell.setCellValue(srcCell.getNumericCellValue());
}
} else if (srcCellType == HSSFCell.CELL_TYPE_STRING) {
distCell.setCellValue(srcCell.getRichStringCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) {
// nothing21
} else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) {
distCell.setCellValue(srcCell.getBooleanCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) {
distCell.setCellErrorValue(srcCell.getErrorCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) {
distCell.setCellFormula(srcCell.getCellFormula());
} else { // nothing29
}
}
}
}

2.用微软的API实现复制:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection; namespace UnitTestProject1
{
[TestClass]
public class UnitTestExcel
{
[TestMethod]
public void TestMethod1()
{ Excel.Application app = new Excel.Application(); Excel.Workbook workbook1 = app.Workbooks._Open("C:\\a.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Excel.Worksheet sheet1 = workbook1.Worksheets["Sheet1"] as Excel.Worksheet; Excel.Workbook workbook2 = app.Workbooks._Open("C:\\a1.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Excel.Worksheet sheet2 = workbook2.Worksheets["Sheet1"] as Excel.Worksheet; sheet2.Copy(Type.Missing, sheet1); workbook1.Save();
workbook1.Close(false, Type.Missing, Type.Missing);
workbook2.Close(false, Type.Missing, Type.Missing);
}
}
}

关于C#操作Excel,复制Sheet的记录的更多相关文章

  1. POI解析读写EXCEL,复制SHEET,兼容EXCEL93-2003,2007

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apa ...

  2. VBS操作Excel常见方法

    VBS操作Excel常见方法 作者: 字体:[增加 减小] 类型:转载 时间:2009-11-13我要评论 VBS控制Excel常见方法,需要的朋友可以参考下. dim oExcel,oWb,oShe ...

  3. 使用NPOI操作Excel(03、07)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...

  4. 用python库openpyxl操作excel,从源excel表中提取信息复制到目标excel表中

    现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法 ...

  5. Python操作Excel删除一个Sheet

    在使用Python进行数据分析处理,操作Excel,有时需要删除某个Excel里的某个sheet,这里记录一个我测试成功的一个办法 软件环境: 1.OS:Win 10 64位 2.Python 3.7 ...

  6. 记录python接口自动化测试--把操作excel文件的方法封装起来(第五目)

    前面补充了如何来操作excel文件,这次把如何获取excel文件的sheet对象.行数.单元格数据的方法进行封装,方便后面调用 handle_excel.py# coding:utf-8 import ...

  7. Excel的Sheet页复制

    最近在做一个项目,其中涉及基于模板对Excel的Sheet页进行复制.在网上尝试了很多,发现都不够完美,苦恼. 然后在查阅资料的过程中,发现有一篇提及,POI的API只对同一个Excel文件中的She ...

  8. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  9. vfp 操作excel

    VFP全面控制EXCEL 收藏 VFP和Excel都可以用来进行处理数据库表格,如果巧妙地将二者的优点结合起来,将会大大方便我们的工作.比如我们可以利用VFP进行处理数据,而利用Excel的预览打印功 ...

随机推荐

  1. nginx rewrite 导致验证码不正确

    配置nginx里url rewrite的时候,为了使浏览器地址栏的URL保持不变, 使用proxy_pass反向代理,但发现每次都会生成新的jsessionid 解决方法,配置中增加 proxy_co ...

  2. ACM学习历程—HDU1028 Ignatius and the Princess(组合数学)

    Ignatius and the Princess Description        "Well, it seems the first problem is too easy. I w ...

  3. [转载]Windows网络编程系列教程之四:Select模型

    原文:http://www.51see.com/asp/bbs/public/bp_show.asp?t_id=200308131152297103 讲一下套接字模式和套接字I/O模型的区别.先说明一 ...

  4. window系统的HOST详解

    很多用户都知道在Window系统中有个Hosts文件(没有后缀名),在Windows 98系统下该文件在Windows目录,在Windows 2000/XP系统中位于C\Winnt\System32 ...

  5. CF 438 E & bzoj 3625 小朋友和二叉树 —— 多项式开方

    题目:http://codeforces.com/contest/438/problem/E https://www.lydsy.com/JudgeOnline/problem.php?id=3625 ...

  6. 几种开源SIP协议栈对比

    几种开源SIP协议栈对比 随着VoIP和NGN技术的发展,H.323时代即将过渡到SIP时代,在H.323的开源协议栈中,Openh323占统治地位,它把一个复杂而又先进的H.323协议栈展现在普通程 ...

  7. VS2008 查找失效怎么办

    按Ctrl+F没有反应?   visual studio 里 查找替换 显示不出来;   还能用 让他查找个不存在的文本还会弹出找不到的提示; 就是看不到 查找替换的操作框了;   问题解决方法:   ...

  8. hash tree

    http://en.wikipedia.org/wiki/Hash_list In computer science, a hash list is typically a list of hashe ...

  9. HDU - 6185 Covering(暴搜+递推+矩阵快速幂)

    Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...

  10. js.alert(重写)

    function dialogFn(Msg, btnOkCallBack, btnCancelCallBack) { $("body").append('<div id=&q ...