max-width、max-height以及這兩個的 min 版本是偶爾能在某些CSS中看到的屬性設定
不過因為IE不會辨識這些屬性,所以就直接寫JavaScript 調整圖片大小
var img = this.getElementsByTagName("img")[0];
img.onload = function(){
var maxWidth = "300", maxHeight = "80";
if(img.offsetHeight >= (maxHeight / maxWidth) * img.offsetWidth){
img.style.width = img.offsetWidth / img.offsetHeight * maxHeight + "px";
img.style.height = maxHeight + "px";
}else{
img.style.height = img.offsetHeight / img.offsetWidth * maxHeight + "px";
img.style.width = maxWidth + "px";
}
}
2012年10月30日 星期二
2012年9月20日 星期四
POI 自訂方法
copyRow() - 複製Row的method
updateFormula() - 計算Excel公式結果的方法
updateFormula() - 計算Excel公式結果的方法
POI有cloneSheet()這個複製sheet的方便方法
但是卻缺少複製sheet裡的row的方法
當然考量到公式的計算與合併的儲存格的位置
這個方法須自訂是不太令人意外
private void copyRow(HSSFSheet worksheet, int sourceRowNum, int destinationRowNum) {
但是卻缺少複製sheet裡的row的方法
當然考量到公式的計算與合併的儲存格的位置
這個方法須自訂是不太令人意外
private void copyRow(HSSFSheet worksheet, int sourceRowNum, int destinationRowNum) {
// Get the source & new row
HSSFRow newRow = worksheet.getRow(destinationRowNum);
HSSFRow sourceRow = worksheet.getRow(sourceRowNum);
// If the row exist in destination, push down all rows by 1 else create a new row
if (newRow != null) {
worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
} else {
newRow = worksheet.createRow(destinationRowNum);
}
// Loop through source columns to add to new row
for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
// Grab a copy of the old/new cell
HSSFCell oldCell = sourceRow.getCell(i);
HSSFCell newCell = newRow.createCell(i);
// If the old cell is null jump to next cell
if (oldCell == null) {
newCell = null;
continue;
}
// Use old cell style
newCell.setCellStyle(oldCell.getCellStyle());
// If there is a cell comment, copy
if (newCell.getCellComment() != null) {
newCell.setCellComment(oldCell.getCellComment());
}
// If there is a cell hyperlink, copy
if (oldCell.getHyperlink() != null) {
newCell.setHyperlink(oldCell.getHyperlink());
}
// Set the cell data type
newCell.setCellType(oldCell.getCellType());
// Set the cell data value
switch (oldCell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
newCell.setCellValue(oldCell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(oldCell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:
newCell.setCellErrorValue(oldCell.getErrorCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(oldCell.getCellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
newCell.setCellValue(oldCell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(oldCell.getRichStringCellValue());
break;
}
}
// If there are are any merged regions in the source row, copy to new row
for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
CellRangeAddress newCellRangeAddress =
new CellRangeAddress(newRow.getRowNum(),
new CellRangeAddress(newRow.getRowNum(),
(newRow.getRowNum() +
(cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())
),
(cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())
),
cellRangeAddress.getFirstColumn(),
cellRangeAddress.getLastColumn());
worksheet.addMergedRegion(newCellRangeAddress);
}
}
}
計算Excel公式結果的方法
使用POI填入公式時,Excel表並不會將計算後的結果立刻展示
它的作法是顯示出公式(舊版office)或待開啟編輯權限後才計算(office 2010)
以下方法能計算特定行數的Excel公式
private void updateFormula(HSSFWorkbook wb, HSSFSheet sheet, int row){
Row r = sheet.getRow(row);
Cell cell = null;
HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
for(int i = r.getFirstCellNum(); i < r.getLastCellNum(); i++){
cell = r.getCell(i);
if(cell.getCellType()== Cell.CELL_TYPE_FORMULA)
eval.evaluateFormulaCell(cell);
}
}
HSSFFormulaEvaluator提供了evaluateFormulaCell(Cell cell)方法
計算公式保存結果,但不改變公式
而evaluateInCell(Cell cell)方法是計算公式,並將原公式替換為計算結果,
也就是說該單元格的類型從Cell.CELL_TYPE_FORMULA改為Cell.CELL_TYPE_NUMBERIC
HSSFFormulaEvaluator提供了靜態方法evaluateAllFormulaCells(HSSFWorkbook wb)
計算一個Excel文件的所有公式,用起來很方便
計算Excel公式結果的方法
使用POI填入公式時,Excel表並不會將計算後的結果立刻展示
它的作法是顯示出公式(舊版office)或待開啟編輯權限後才計算(office 2010)
以下方法能計算特定行數的Excel公式
private void updateFormula(HSSFWorkbook wb, HSSFSheet sheet, int row){
Row r = sheet.getRow(row);
Cell cell = null;
HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
for(int i = r.getFirstCellNum(); i < r.getLastCellNum(); i++){
cell = r.getCell(i);
if(cell.getCellType()== Cell.CELL_TYPE_FORMULA)
eval.evaluateFormulaCell(cell);
}
}
HSSFFormulaEvaluator提供了evaluateFormulaCell(Cell cell)方法
計算公式保存結果,但不改變公式
而evaluateInCell(Cell cell)方法是計算公式,並將原公式替換為計算結果,
也就是說該單元格的類型從Cell.CELL_TYPE_FORMULA改為Cell.CELL_TYPE_NUMBERIC
HSSFFormulaEvaluator提供了靜態方法evaluateAllFormulaCells(HSSFWorkbook wb)
計算一個Excel文件的所有公式,用起來很方便
2010年12月8日 星期三
範例 - 刪除目錄及其底下所有文件
public void deleteAll(File path){
if(path.exists()){
if(path.isFile()){
path.delete();
}else{
File[] files = path.listFiles();
for(int i = 0; i < files.length; i++){
deleteAll(files[i]);
}
path.delete();
}
}
}
if(path.exists()){
if(path.isFile()){
path.delete();
}else{
File[] files = path.listFiles();
for(int i = 0; i < files.length; i++){
deleteAll(files[i]);
}
path.delete();
}
}
}
範例 - 計算硬碟中的資料大小
int uAppendSize = 0; //store uploaded data size info
File[] fList =TargetDir.listFiles();
for (int j = 0; j < fList.length; j++){
FileInputStream in = new FileInputStream(fList[j]);
uAppendSize += in.available();
in.close(); //must close FileInputStream after use it
}
這個範例會檢查寫入檔案所在的資料夾,並加該資料夾內的所有檔案大小相加
FileInputStream 開啟後務必要記得關閉
否則檔案的刪除、修改等動作會因為檔案被FileInputStream佔用而失敗
File[] fList =TargetDir.listFiles();
for (int j = 0; j < fList.length; j++){
FileInputStream in = new FileInputStream(fList[j]);
uAppendSize += in.available();
in.close(); //must close FileInputStream after use it
}
這個範例會檢查寫入檔案所在的資料夾,並加該資料夾內的所有檔案大小相加
FileInputStream 開啟後務必要記得關閉
否則檔案的刪除、修改等動作會因為檔案被FileInputStream佔用而失敗
2010年11月7日 星期日
自訂編碼標籤範例
如果不將Eclipse的環境編碼改成UTF-8,似乎轉碼會有問題
因應這種情況寫了自訂標籤來協助處理
encrypt.tld=========
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.2</tlib-version>
<short-name>encryptlib</short-name>
<uri>encrypt-tags</uri>
<tag>
<description>URL get/post encrypting</description>
<name>encrypt</name>
<tag-class>tw.vencs.EncrptTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>encryptValue</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
EncrptTag.java===============
package tw.vencs;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.JspException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class EncrptTag extends SimpleTagSupport{
private String encryptValue;
public void doTag() throws JspException {
try {
encryptValue = new String(encryptValue.getBytes("iso-8859-1"),"UTF-8");
getJspContext().getOut().write(encryptValue);
} catch (IOException e) {
Logger.getLogger(EncrptTag.class.getName()).log(Level.SEVERE, null, e);
throw new RuntimeException();
}
}
public void setEncryptValue(String encryptValue) {
this.encryptValue = encryptValue;
}
}
Demo.jsp===============
.....
<%@ taglib prefix="e" uri="encrypt-tags" %>
.....
<body>
.....
....=<e:encrypt encryptValue="${param.itemName}"/>
</body>
.....
因應這種情況寫了自訂標籤來協助處理
encrypt.tld=========
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.2</tlib-version>
<short-name>encryptlib</short-name>
<uri>encrypt-tags</uri>
<tag>
<description>URL get/post encrypting</description>
<name>encrypt</name>
<tag-class>tw.vencs.EncrptTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>encryptValue</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
EncrptTag.java===============
package tw.vencs;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.JspException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class EncrptTag extends SimpleTagSupport{
private String encryptValue;
public void doTag() throws JspException {
try {
encryptValue = new String(encryptValue.getBytes("iso-8859-1"),"UTF-8");
getJspContext().getOut().write(encryptValue);
} catch (IOException e) {
Logger.getLogger(EncrptTag.class.getName()).log(Level.SEVERE, null, e);
throw new RuntimeException();
}
}
public void setEncryptValue(String encryptValue) {
this.encryptValue = encryptValue;
}
}
Demo.jsp===============
.....
<%@ taglib prefix="e" uri="encrypt-tags" %>
.....
<body>
.....
....=<e:encrypt encryptValue="${param.itemName}"/>
</body>
.....
訂閱:
意見 (Atom)