導航:首頁 > 文件類型 > poi導出excel文件會加密

poi導出excel文件會加密

發布時間:2023-03-11 22:41:57

java POI 生成Excel 加密但不可以顯示只讀按鈕 可以嗎

你好,請問你這個問題解決了嗎?能共享一下不?只讀按鈕是怎麼去掉的,謝謝!!

Ⅱ 誰能給我一個詳細的Java通過Apache POI導出Excel方法,最好能給完整代碼

package com.test;
/**
* 需要的jar包:
* poi-3.0.2-FINAL-20080204.jar
* poi-contrib-3.0.2-FINAL-20080204.jar
*poi-scratchpad-3.0.2-FINAL-20080204.jar
* poi-3.5-beta6-20090622.jar
* geronimo-stax-api_1.0_spec-1.0.jar
* ooxml-schemas-1.0.jar
* openxml4j-bin-beta.jar
* poi-ooxml-3.5-beta6-20090622.jar
*xmlbeans-2.3.0.jar
* dom4j-1.6.1.jar
*/
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.POIOLE2TextExtractor;
import org.apache.poi.POITextExtractor;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.wordExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;
public class WordAndExcelExtractor {
public static void main(String[] args) {
try {
// 讀取word
String wordFile = "D:/1.doc";
//String wordText2007 = WordAndExcelExtractor.extractTextFromDOC2007(wordFile);
//System.out.println("wordText2007=======" + wordText2007);
InputStream isword = new FileInputStream(wordFile);
WordExtractor wordExtractor = new WordExtractor(isword);
System.out.println("word========" + wordExtractor.getText());
// 讀取 Excel
InputStream is = new FileInputStream("D:/測試.xls");
String excelText = WordAndExcelExtractor.extractTextFromXLS(is);
System.out.println("text2003==========" + excelText);
String excelFile = "D:/test2.xlsx";
String excelText2007 = WordAndExcelExtractor
.extractTextFromXLS2007(excelFile);
System.out.println("excelText2007==========" + excelText2007);
// 讀取 PPT
PowerPointExtractor ppe = new PowerPointExtractor("D:/test.ppt");
System.out.println("ppt2003===============" + ppe.getText());
// System.out.println("###############################");
// System.out.println(ppe.getText(true, true, true, true));
//
// InputStream is = new FileInputStream("D:/test.ppt");
// PowerPointExtractor ppt2003 = new PowerPointExtractor(is);
// System.out.println(ppt2003.getText());
System.out.println("************************************");
XSLFPowerPointExtractor ppt = new XSLFPowerPointExtractor(
POIXMLDocument.openPackage("D:/test2.pptx"));
System.out.println("ppt2007============================="
+ ppt.getText());
/* 讀取PDF */
InputStream in = new FileInputStream("D:/test.pdf");
PDDocument pdfDocument = PDDocument.load(in);
if (pdfDocument.isEncrypted()) {
// 僅僅嘗試使用默認密碼打開加密的PDF
pdfDocument.decrypt("");
}
PDFTextStripper stripper = null;
// 創建一個writer用來作來存儲文件正文
StringWriter writer = new StringWriter();
if (stripper == null) {
stripper = new PDFTextStripper();
} else {
stripper.resetEngine();
}
stripper.writeText(pdfDocument, writer);
String contents = writer.getBuffer().toString();
System.out.println("pdfd===" + contents);

/* 向Word中寫入數據 */
byte[] a = contents.getBytes();
ByteArrayInputStream bs = new ByteArrayInputStream(a);
POIFSFileSystem fs = new POIFSFileSystem();
// /////////////////////////////////
DirectoryEntry directory = fs.getRoot();
DocumentEntry de = directory.createDocument("WordDocument", bs);
// 以上兩句代碼不能省略,否則輸出的是亂碼
FileOutputStream fos = new FileOutputStream("D:\\dd.doc");
fs.writeFilesystem(fos);
bs.close();
fos.flush();
fos.close();
System.out.println("寫入成功");

} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Method: extractTextFromXLS
* @Description: 從excel 2003檔中提取純文本
*
* @param
* @return String
* @throws
*/
@SuppressWarnings("deprecation")
private static String extractTextFromXLS(InputStream is) throws IOException {
StringBuffer content = new StringBuffer();
HSSFWorkbook workbook = new HSSFWorkbook(is); // 創建對Excel工作簿文件的引用
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets); // 獲得一個sheet
content.append(aSheet.getSheetName());
content.append("\r\n-----------------------\r\n");
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet
.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 獲得一行
for (short cellNumOfRow = 0; cellNumOfRow <= aRow
.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow); // 獲得列值
if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
content.append(aCell.getNumericCellValue());
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
content.append(aCell.getBooleanCellValue());
} else {
content.append(aCell.getStringCellValue());
}
content.append("\t");
}
}
content.append("\r\n");
}
}
}
}
return content.toString();
}
/**
* @Method: extractTextFromXLS2007
* @Description: 從excel 2007文檔中提取純文本
*
* @param
* @return String
* @throws
*/
private static String extractTextFromXLS2007(String fileName)
throws Exception {
StringBuffer content = new StringBuffer();
// 構造 XSSFWorkbook 對象,strPath 傳入文件路徑
XSSFWorkbook xwb = new XSSFWorkbook(fileName);
// 循環工作表Sheet
for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) {
XSSFSheet xSheet = xwb.getSheetAt(numSheet);
if (xSheet == null) {
continue;
}
// 循環行Row
for (int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++) {
XSSFRow xRow = xSheet.getRow(rowNum);
if (xRow == null) {
continue;
}
// 循環列Cell
for (int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++) {
XSSFCell xCell = xRow.getCell(cellNum);
if (xCell == null) {
continue;
}
if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
content.append(xCell.getBooleanCellValue());
} else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
content.append(xCell.getNumericCellValue());
} else {
content.append(xCell.getStringCellValue());
}
}
}
}
return content.toString();
}
}

這是POI jar包的下載地址,我下載的是3.9版本
http://poi.apache.org/download.html

Ⅲ java 使用POI導出Excel文件,打開導出文件時報錯

在保護狀態下execl的格式有可能正在被使用,你這邊修改,准確說是線程沖突,一般excel值會作為導出文件的模板,是不會編輯的。你可以在讀的時候判斷execl是否正在被使用。
下面的代碼問題,你可以參考
package com.hwt.glmf.common;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;

/**
* 導出Excel公共方法
* @version 1.0
*
* @author wangcp
*
*/
public class ExportExcel extends BaseAction {

//顯示的導出表的標題
private String title;
//導出表的列名
private String[] rowName ;

private List<Object[]> dataList = new ArrayList<Object[]>();

HttpServletResponse response;

//構造方法,傳入要導出的數據
public ExportExcel(String title,String[] rowName,List<Object[]> dataList){
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
}

/*
* 導出數據
* */
public void export() throws Exception{
try{
HSSFWorkbook workbook = new HSSFWorkbook(); // 創建工作簿對象
HSSFSheet sheet = workbook.createSheet(title); // 創建工作表

// 產生表格標題行
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTiltle = rowm.createCell(0);

//sheet樣式定義【getColumnTopStyle()/getStyle()均為自定義方法 - 在下面 - 可擴展】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//獲取列頭樣式對象
HSSFCellStyle style = this.getStyle(workbook); //單元格樣式對象

sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1)));
cellTiltle.setCellStyle(columnTopStyle);
cellTiltle.setCellValue(title);

// 定義所需列數
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置創建行(最頂端的行開始的第二行)

// 將列頭設置到sheet的單元格中
for(int n=0;n<columnNum;n++){
HSSFCell cellRowName = rowRowName.createCell(n); //創建列頭對應個數的單元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //設置列頭單元格的數據類型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //設置列頭單元格的值
cellRowName.setCellStyle(columnTopStyle); //設置列頭單元格樣式
}

//將查詢出的數據設置到sheet對應的單元格中
for(int i=0;i<dataList.size();i++){

Object[] obj = dataList.get(i);//遍歷每個對象
HSSFRow row = sheet.createRow(i+3);//創建所需的行數

for(int j=0; j<obj.length; j++){
HSSFCell cell = null; //設置單元格的數據類型
if(j == 0){
cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i+1);
}else{
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(!"".equals(obj[j]) && obj[j] != null){
cell.setCellValue(obj[j].toString()); //設置單元格的值
}
}
cell.setCellStyle(style); //設置單元格樣式
}
}
//讓列寬隨著導出的列長自動適應
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//當前行未被使用過
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}

if(workbook !=null){
try
{
String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
String headStr = "attachment; filename=\"" + fileName + "\"";
response = getResponse();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", headStr);
OutputStream out = response.getOutputStream();
workbook.write(out);
}
catch (IOException e)
{
e.printStackTrace();
}
}

}catch(Exception e){
e.printStackTrace();
}

}

/*
* 列頭單元格樣式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {

// 設置字體
HSSFFont font = workbook.createFont();
//設置字體大小
font.setFontHeightInPoints((short)11);
//字體加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體名字
font.setFontName("Courier New");
//設置樣式;
HSSFCellStyle style = workbook.createCellStyle();
//設置底邊框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//設置底邊框顏色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//設置左邊框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//設置左邊框顏色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//設置右邊框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//設置右邊框顏色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//設置頂邊框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//設置頂邊框顏色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在樣式用應用設置的字體;
style.setFont(font);
//設置自動換行;
style.setWrapText(false);
//設置水平對齊的樣式為居中對齊;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//設置垂直對齊的樣式為居中對齊;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}

/*
* 列數據信息單元格樣式
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 設置字體
HSSFFont font = workbook.createFont();
//設置字體大小
//font.setFontHeightInPoints((short)10);
//字體加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體名字
font.setFontName("Courier New");
//設置樣式;
HSSFCellStyle style = workbook.createCellStyle();
//設置底邊框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//設置底邊框顏色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//設置左邊框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//設置左邊框顏色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//設置右邊框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//設置右邊框顏色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//設置頂邊框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//設置頂邊框顏色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在樣式用應用設置的字體;
style.setFont(font);
//設置自動換行;
style.setWrapText(false);
//設置水平對齊的樣式為居中對齊;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//設置垂直對齊的樣式為居中對齊;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}
}

Ⅳ poi導出Excel問題

你的方法應該是沒有問題,404錯誤只是你的跳轉沒響應,估計跳轉頁面路徑錯了。沒有你的配置文件不好說

閱讀全文

與poi導出excel文件會加密相關的資料

熱點內容
win10如何查找本機文件在哪裡設置密碼 瀏覽:124
視頻放到ps3哪個文件夾 瀏覽:389
一列有多少項數據 瀏覽:483
蘋果5英版卡貼怎麼調 瀏覽:742
網路快的城市有哪些 瀏覽:481
刻錄時源文件名與文件名 瀏覽:239
安卓app遠程下發配置文件 瀏覽:898
如何編程qq收到信息 瀏覽:959
飛思卡爾智能車程序分為幾個模塊 瀏覽:960
怎麼深入了解編程語言 瀏覽:942
win10editionbeta 瀏覽:812
蘋果手機常用軟體大全 瀏覽:172
蘋果7晶元型號8010 瀏覽:1
怎麼解壓視頻文件 瀏覽:631
華為共享網路如何設置密碼 瀏覽:272
粉碎文件刪除磁碟空間什麼意思 瀏覽:683
js創建div設置屬性 瀏覽:81
甜甜圈app怎麼樣 瀏覽:757
mc伺服器載入配置文件指令 瀏覽:716
gpsnvmbin是什麼文件 瀏覽:161

友情鏈接