当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

[简单]poi删除excel 2007超链接

发表于: 2015-03-28   作者:53873039oycg   来源:转载   浏览:
摘要:       采用解析sheet.xml方式删除超链接,缺点是要打开文件2次,代码如下:      public void removeExcel2007AllHyperLink(String filePath) throws Exception { OPCPackage ocPkg = OPCPac

      采用解析sheet.xml方式删除超链接,缺点是要打开文件2次,代码如下:

    

public void removeExcel2007AllHyperLink(String filePath) throws Exception {
		OPCPackage ocPkg = OPCPackage.open(new FileInputStream(filePath));
		Workbook wb = new XSSFWorkbook(ocPkg);
		int totalSheet = wb.getNumberOfSheets();
		String savePath = "f:/saveFile/temp/sys_xlsx_"+ System.currentTimeMillis() + ".xlsx";
		CellStyle cellStype=wb.createCellStyle();
		Font defaultFont = wb.createFont();
		defaultFont.setColor(IndexedColors.BLACK.getIndex());
		defaultFont.setUnderline(Font.U_NONE);
		cellStype.setFont(defaultFont);
		for (int i = 0; i < totalSheet; i++) {
			Sheet sheet = wb.getSheetAt(i);
			int rowCount = sheet.getLastRowNum();
			for (int r = 0; r <= rowCount; r++) {
				Row row = sheet.getRow(r);
				if (row != null) {
					int lastCellNum = row.getLastCellNum();
					Cell cell = null;
					for (int c = 0; c <= lastCellNum; c++) {
						cell = row.getCell(c);
						if (cell == null) {
							continue;
						}
						Hyperlink hylink = cell.getHyperlink();
						if (hylink != null) {
							RichTextString richStr = new XSSFRichTextString(cell
									.getRichStringCellValue().getString());
							richStr.applyFont(defaultFont);
							cell.setCellValue(richStr);
						}
					}
				}
			}
		}
		saveWorkBook(wb, savePath);
		ocPkg = OPCPackage.open(new FileInputStream(savePath));
		for (int sheetIndex = 0; sheetIndex < totalSheet; sheetIndex++) {
			List<PackagePart> rs = ocPkg.getPartsByName(Pattern
					.compile("/xl/worksheets/sheet" + (sheetIndex + 1)
							+ "\\.xml"));
			for (PackagePart pkgPart : rs) {
				if (pkgPart instanceof ZipPackagePart) {
					ZipPackagePart zipPart = (ZipPackagePart) pkgPart;
					Document doc = getDocument(zipPart.getInputStream());
					if (doc != null) {
						NodeList hyperlinks = doc
								.getElementsByTagName("hyperlinks");
						for (int i = hyperlinks.getLength() - 1; i >= 0; i--) {
							Node hyperlink = hyperlinks.item(i);
							hyperlink.getParentNode().removeChild(hyperlink);
						}
						StreamHelper.saveXmlInStream(doc,
								zipPart.getOutputStream());
					}
				}
			}
		}
		wb = new XSSFWorkbook(ocPkg);
		saveWorkBook(wb, savePath);
	}

	public Document getDocument(InputStream inStream) {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		Document doc = null;
		try {
			db = dbf.newDocumentBuilder();
			doc = db.parse(inStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}

	public void saveWorkBook(Workbook wb, String filePath) throws Exception {
		FileOutputStream fileOut = new FileOutputStream(filePath);
		wb.write(fileOut);
		fileOut.close();
	}

   全文完。

 

[简单]poi删除excel 2007超链接

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
结果为: 代码如下: import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.Cell; i
使用改变超链接位置的方式实现取消效果,效果如下。 取消后: 代码如下: public void deleteExcel200
代码参考了http://stackoverflow.com/questions/7007810/how-to-create-a-email-link-in-poi-word-f
鉴定Excel格式是否是2003或2007,如果你的做法是读取扩展名,那说明你还是一个菜鸟!因为后缀完全是
import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.Reader; import
word 2007 Wingdings字符操作步骤如下图: 代码很简单: import java.io.FileOutputStream; import ja
我们在做用POI读物 Excel文件时,往往会忽略了Excel的版本,到底是2003还是2007。于是在读取或写入E
所需包: package com.excel.poi; import java.io.File; import java.io.FileInputStream; import j
使用 Apche poi 简单读写 Excel 首先:毋庸置疑,我们得去官网下载POI,地址:http://jakarta.apach
对应的类型有对应的值如何判断,方法如下: cell.getCellType() 获取对应格式的值(int)。对比上面的
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号