关于C#操作Excel,复制Sheet的记录
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的记录的更多相关文章
- POI解析读写EXCEL,复制SHEET,兼容EXCEL93-2003,2007
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apa ...
- VBS操作Excel常见方法
VBS操作Excel常见方法 作者: 字体:[增加 减小] 类型:转载 时间:2009-11-13我要评论 VBS控制Excel常见方法,需要的朋友可以参考下. dim oExcel,oWb,oShe ...
- 使用NPOI操作Excel(03、07)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...
- 用python库openpyxl操作excel,从源excel表中提取信息复制到目标excel表中
现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法 ...
- Python操作Excel删除一个Sheet
在使用Python进行数据分析处理,操作Excel,有时需要删除某个Excel里的某个sheet,这里记录一个我测试成功的一个办法 软件环境: 1.OS:Win 10 64位 2.Python 3.7 ...
- 记录python接口自动化测试--把操作excel文件的方法封装起来(第五目)
前面补充了如何来操作excel文件,这次把如何获取excel文件的sheet对象.行数.单元格数据的方法进行封装,方便后面调用 handle_excel.py# coding:utf-8 import ...
- Excel的Sheet页复制
最近在做一个项目,其中涉及基于模板对Excel的Sheet页进行复制.在网上尝试了很多,发现都不够完美,苦恼. 然后在查阅资料的过程中,发现有一篇提及,POI的API只对同一个Excel文件中的She ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- vfp 操作excel
VFP全面控制EXCEL 收藏 VFP和Excel都可以用来进行处理数据库表格,如果巧妙地将二者的优点结合起来,将会大大方便我们的工作.比如我们可以利用VFP进行处理数据,而利用Excel的预览打印功 ...
随机推荐
- k8s-应用快速入门(ma)
[root@k8s-master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 deplo ...
- python爬虫知识点总结(二)爬虫的基本原理
一.什么是爬虫? 答:请求网页并提取数据的自动化程序. 二.爬虫的基本流程 三.什么是Request和Response? 1.Request 2.Response 四.能抓取怎样的数据 五.解析方式 ...
- bzoj1055玩具取名——区间DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间DP,注意初始化!! 因为没记忆化,TLE了一晚上,区间DP尤其要注意不重复递归! ...
- 如何让Surface RT支持网站的flash
Go to the desktop version of IE10, hit the click ALT button on your keyboard, click on and then Comp ...
- nagios客户端安装与配置windows篇
一.被监控的windows xp客户端的配置 1.安装NSClient++并安装下载地址: http://sourceforge.net/projects/nscplusNSClient++-0.3. ...
- failed to create rwlayer: lstat /var/lib/docker/overlay2/ no such file or directory
在使用Docker构建微服务镜像时出现的错误.第一天构建好好的,第二天就出现了这样的错误.通过百度这条错误的信息非常少,只在 stackoverflow.com 上找到一条,问题指向了 dockerf ...
- Javascript Tip(!!)
var a:var b=!!a; a默认是undefined.!a是true,!!a则是false,所以b的值是false,而不再是undefined,也非其它值,主要是为后续判断提供便利. !!一般 ...
- xgene:WGS,突变与癌,RNA-seq,WES
人类全基因组测序06 SNP(single nucleotide polymorphism):有了10倍以上的覆盖深度以后,来确认SNP信息,就相当可靠了. 一个普通黄种人的基因组,与hg19这个参 ...
- Django 的 路由系统
Django 的路由系统 URL 配置(URLconf)就像Django 锁支撑网站的目录. 它的本质就是URL 与要为该URL 调用的视图函数之间的映射表. 你就是以这种方式告诉Django, 对于 ...
- 洛谷P3382 【模板】三分法(三分找凹凸点)
P3382 [模板]三分法 题目描述 如题,给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减.试求出x的值. 输入输出格式 输入格式: 第一行一次包含一个 ...