⑴ java與xsl樣式表
XSL提供兩種機制來聯合樣式表:
1.樣式表導入,允許樣式表之間相互引用
2.樣式表包含,允許樣式表被原文組合.
樣式表導入
一個XSL樣式表可以包含xsl:import元素.所有xsl:import元素必須出現在樣式表的開頭. xsl:import元素有一個 href 屬性,它的值就表示要導入的樣式表的URI. 相對URI是指相對於xsl:import元素的基URI.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:import href="article.xsl"/>
<xsl:import href="bigfont.xsl"/>
<xsl:define-attribute-set name="note-style">
<xsl:attribute-set font-posture="italic"/>
</xsl:define-attribute-set>
</xsl:stylesheet>
導向的樣式表中的規則和定義比任何被導入樣式表中的規則和定義都要重要.同樣,一被導入的樣式表中的規則和定義比之前導入的樣式表中的規則和定義都要重要.一般來說,更重要的規則或定義比次重要的規則或定義要優先.每一類的規則和定義都會詳細規定它.
樣式表包含
一個樣式表中可以用xsl:include元素來包含另一個XSL樣式表. xsl:include也有href 屬性,它的值就表示被包含的樣式表的URI. 相對URI是指相對於xsl:include元素的基URI. xsl:include元素可以作為xsl:stylesheet元素的子元素,出現在任何xsl:import之後.在XML樹的層次在上該包含生效.由href屬性值定位的資源內容作為一個XML文檔解析,在該文檔中的xsl:stylesheet元素的子元素替代包含文檔的xsl:include元素.同時在被包含的文檔的xsl:import元素在包含文檔中移上至任一存在的xsl:import元素之後. 不象xsl:import,被包含的規則或定義不影響他們被處理的方式.
嵌入樣式表
通常一個樣式表就是一個完整的XML文檔,xsl:stylesheet元素作為文檔的元素. 然而一個XSL樣式表也可以嵌入在其它文檔內容之中.內嵌的方式可能有兩種:XSL樣式表可以原文嵌入在一個非XML文檔中或者xsl:stylesheet不作為文檔元素出現在一個XML文檔中.在第二種情況增加了出現內嵌樣式,即自己規定樣式的文檔的可能. XSL還沒有為之定義相應的機制.這是由於可以採用把樣式表結合文檔的通用方式來實現,只要滿足:
1. 該方式允許一部分內容可以規定為樣式表,例如使用有片段標識符URI
2. 該方式本身能被嵌入在文檔中, 比如作為一個處理指令.定義這樣的方式不在XSL的范圍之內.
下例表明了怎樣用xml:stylesheet處理指令將樣式表和文檔結合來實現內嵌樣式. 其中的URI在片段標識符中使用了一個Xpointer來確定xsl:stylesheet元素的位置.
<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="#id(style1)"?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc>
<head>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" id="style1">
<xsl:import href="doc.xsl"/>
<xsl:template match="id(foo)">
<fo:block font-weight="bold"><xsl:process-children/></fo:block>
</xsl:template>
</xsl:stylesheet>
</head>
<body>
<para id="foo">
...
</para>
</body>
</doc>
⑵ java web工程里xml+xsl路徑問題
寫全路徑/files/xsl/xxxx
或者相對路徑/xsl/xxxx
⑶ java用xsl實現了xml文件有樣式的展現,火狐和ie有樣式,為什麼chrome直接將xml文件下載了
chrome這點比較渣。
⑷ java怎麼讀取excel文件
參考代碼及注釋如下:
importJava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStream;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.ss.usermodel.Cell;
importorg.apache.poi.ss.usermodel.Row;
importorg.apache.poi.ss.usermodel.Sheet;
importorg.apache.poi.ss.usermodel.Workbook;
publicclassReadExcel{
publicstaticvoidreadExcel(Filefile){
try{
InputStreaminputStream=newFileInputStream(file);
StringfileName=file.getName();
Workbookwb=null;
//poi-3.9.jar只可以讀取2007以下的版本,後綴為:xsl
wb=newHSSFWorkbook(inputStream);//解析xls格式
Sheetsheet=wb.getSheetAt(0);//第一個工作表,第二個則為1,以此類推...
intfirstRowIndex=sheet.getFirstRowNum();
intlastRowIndex=sheet.getLastRowNum();
for(intrIndex=firstRowIndex;rIndex<=lastRowIndex;rIndex++){
Rowrow=sheet.getRow(rIndex);
if(row!=null){
intfirstCellIndex=row.getFirstCellNum();
//intlastCellIndex=row.getLastCellNum();
//此處參數cIndex決定可以取到excel的列數。
for(intcIndex=firstCellIndex;cIndex<3;cIndex++){
Cellcell=row.getCell(cIndex);
Stringvalue="";
if(cell!=null){
value=cell.toString();
System.out.print(value+" ");
}
}
System.out.println();
}
}
}catch(FileNotFoundExceptione){
//TODO自動生成catch塊
e.printStackTrace();
}catch(IOExceptione){
//TODO自動生成catch塊
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
Filefile=newFile("D:/test.xls");
readExcel(file);
}
}
⑸ java開發中xsl還常用么
常用啊,可以把excel作為小型的資料庫的。
⑹ java中怎麼讀取excel文件
package com.jqgj.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ImportNameTest {
/**
* Excel 2003
*/
private final static String XLS = "xls";
/**
* Excel 2007
*/
private final static String XLSX = "xlsx";
/**
* 分隔符
*/
private final static String SEPARATOR = "|";
/**
* 由Excel文件的Sheet導出至List
*
* @param file
* @param sheetNum
* @return
*/
public static List<String> exportListFromExcel(File file, int sheetNum)
throws IOException {
return exportListFromExcel(new FileInputStream(file),
FilenameUtils.getExtension(file.getName()), sheetNum);
}
/**
* 由Excel流的Sheet導出至List
*
* @param is
* @param extensionName
* @param sheetNum
* @return
* @throws IOException
*/
public static List<String> exportListFromExcel(InputStream is,
String extensionName, int sheetNum) throws IOException {
Workbook workbook = null;
if (extensionName.toLowerCase().equals(XLS)) {
workbook = new HSSFWorkbook(is);
} else if (extensionName.toLowerCase().equals(XLSX)) {
workbook = new XSSFWorkbook(is);
}
return exportListFromExcel(workbook, sheetNum);
}
/**
* 由指定的Sheet導出至List
*
* @param workbook
* @param sheetNum
* @return
* @throws IOException
*/
private static List<String> exportListFromExcel(Workbook workbook,
int sheetNum) {
Sheet sheet = workbook.getSheetAt(sheetNum);
// 解析公式結果
FormulaEvaluator evaluator = workbook.getCreationHelper()
.createFormulaEvaluator();
List<String> list = new ArrayList<String>();
int minRowIx = sheet.getFirstRowNum();
int maxRowIx = sheet.getLastRowNum();
for (int rowIx = minRowIx; rowIx <= maxRowIx; rowIx++) {
Row row = sheet.getRow(rowIx);
StringBuilder sb = new StringBuilder();
short minColIx = row.getFirstCellNum();
short maxColIx = row.getLastCellNum();
for (short colIx = minColIx; colIx <= maxColIx; colIx++) {
Cell cell = row.getCell(new Integer(colIx));
CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
continue;
}
// 經過公式解析,最後只存在Boolean、Numeric和String三種數據類型,此外就是Error了
// 其餘數據類型,根據官方文檔,完全可以忽略http://poi.apache.org/spreadsheet/eval.html
switch (cellValue.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
sb.append(SEPARATOR + cellValue.getBooleanValue());
break;
case Cell.CELL_TYPE_NUMERIC:
// 這里的日期類型會被轉換為數字類型,需要判別後區分處理
if (DateUtil.isCellDateFormatted(cell)) {
sb.append(SEPARATOR + cell.getDateCellValue());
} else {
//把手機號碼轉換為字元串
DecimalFormat df = new DecimalFormat("#");
sb.append(SEPARATOR + df.format(cellValue.getNumberValue()));
}
break;
case Cell.CELL_TYPE_STRING:
sb.append(SEPARATOR + cellValue.getStringValue());
break;
case Cell.CELL_TYPE_FORMULA:
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
break;
default:
break;
}
}
list.add(sb.toString());
}
return list;
}
/**
* @param args
*/
public static void main(String[] args) {
String path = "f:\\telName.xlsx";
try {
List<String> listS= exportListFromExcel(new File(path),0);
for(int i=0;i<listS.size();i++){
System.out.println(listS.get(i));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}