簡單的將String寫入到操作系統的剪貼板中,可以參考: import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; public class WriteString { public static void main(String[] args) { Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard(); setClipboardString(sysc,"寫入剪貼板的String"); } protected static void setClipboardString(Clipboard clip, String writeMe) { Transferable tText = new StringSelection(writeMe); clip.setContents(tText, null); } } 由於拷貝String,jdk中已經有類StringSelection支持,如果要拷貝一個文件,或者目錄,則要參考此類,對其進行修改,類名:FileSelection.java (改的可能不是很成功,但能夠用) 內容為: /* @(#)FileSelection.java 07/05/14 * @author me * Copy file from FoxEDM,Paste to System(e.g:2000 or xp etc) */ package com.foxera.foxlink.ui;import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.*; import java.util.List; import java.util.ArrayList; /** * A <code>Transferable</code> which implements the capability required * to transfer a <code>String</code>. * * This <code>Transferable</code> properly supports * <code>DataFlavor.javaFileListFlavor</code> * and all equivalent flavors. Support for * <code>DataFlavor.plainTextFlavor</code> * and all equivalent flavors is <b>deprecated</b>. No other * <code>DataFlavor</code>s are supported. * * @see java.awt.datatransfer.DataFlavor#javaFileListFlavor * @see java.awt.datatransfer.DataFlavor#plainTextFlavor */ public class FileSelection implements Transferable, ClipboardOwner { private static final int FILE= 0; private static final int PLAIN_FILE = 1; private static final DataFlavor[] flavors = { DataFlavor.javaFileListFlavor, DataFlavor.javaFileListFlavor }; private List data;
/** * Creates a <code>Transferable</code> capable of transferring * the specified <code>String</code>. */ public FileSelection(List data) { this.data = data; } /** * Returns an array of flavors in which this <code>Transferable</code> * can provide the data. <code>DataFlavor.javaFileListFlavor</code> * is properly supported. * Support for <code>DataFlavor.plainTextFlavor</code> is * <b>deprecated</b>. * * @return an array of length two, whose elements are <code>DataFlavor. * stringFlavor</code> and <code>DataFlavor.plainTextFlavor</code> */ public DataFlavor[] getTransferDataFlavors() { // returning flavors itself would allow client code to modify // our internal behavior return (DataFlavor[])flavors.clone(); } /** * Returns whether the requested flavor is supported by this * <code>Transferable</code>. * * @param flavor the requested flavor for the data * @return true if <code>flavor</code> is equal to * <code>DataFlavor.javaFileListFlavor</code> or * <code>DataFlavor.plainTextFlavor</code>; false if <code>flavor</code> * is not one of the above flavors * @throws NullPointerException if flavor is <code>null</code> */ public boolean isDataFlavorSupported(DataFlavor flavor) { // JCK Test StringSelection0003: if 'flavor' is null, throw NPE for (int i = 0; i < flavors.length; i++) { if (flavor.equals(flavors[i])) { return true; } } return false; } /** * Returns the <code>Transferable</code>'s data in the requested * <code>DataFlavor</code> if possible. If the desired flavor is * <code>DataFlavor.javaFileListFlavor</code>, or an equivalent flavor, * the <code>String</code> representing the selection is * returned. If the desired flavor is * <code>DataFlavor.plainTextFlavor</code>, * or an equivalent flavor, a <code>Reader</code> is returned. * <b>Note:</b> The behavior of this method for * </code>DataFlavor.plainTextFlavor</code> * and equivalent <code>DataFlavor</code>s is inconsistent with the * definition of <code>DataFlavor.plainTextFlavor</code>. * * @param flavor the requested flavor for the data * @return the data in the requested flavor, as outlined above * @throws UnsupportedFlavorException if the requested data flavor is * not equivalent to either <code>DataFlavor.stringFlavor</code> * or <code>DataFlavor.plainTextFlavor</code> * @throws IOException if an IOException occurs while retrieving the data. * By default, StringSelection never throws this exception, but a * subclass may. * @throws NullPointerException if flavor is <code>null</code> * @see java.util.ArrayList<E> */ public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.equals(flavors[FILE])) { return (Object)data; } else if (flavor.equals(flavors[PLAIN_FILE])) { return new ArrayList<File>(data == null ? null : data); } else { throw new UnsupportedFlavorException(flavor); } } public void lostOwnership(Clipboard clipboard, Transferable contents) { }}測試代碼為: import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Transferable; import java.util.List; import java.io.File; import java.util.ArrayList; public class WriteFile { public static void main(String[] args) { Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
File file = new File("c:\\1.txt");//目錄也可以 List<File> listFile = new ArrayList<File>(); listFile.add(file); setClipboardFile(sysc,listFile); } protected static void setClipboardFile(Clipboard clip, List writeMe) { Transferable tFile = new FileSelection(writeMe); clip.setContents(tFile, null); } }從操作系統的剪貼板中讀取資料,可以參考: protected static String getClipboardContent(Clipboard clip) throws Exception{ //get content from clipboard Transferable clipT = clip.getContents(null); if (clipT != null) { // check content's type if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) { return (String)clipT.getTransferData(DataFlavor.stringFlavor); } else if(clipT.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { Object ob = clipT.getTransferData(DataFlavor.javaFileListFlavor); List fileList = (List)ob; if((fileList != null)&&(fileList.size() > 0)) { for(int k=0;k<fileList.size();k++) { System.out.println(((File)fileList.get(k)).getAbsolutePath()); //根據路徑拷貝文件 } } } } return null; }
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
public class WriteString
{
public static void main(String[] args)
{
Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
setClipboardString(sysc,"寫入剪貼板的String");
} protected static void setClipboardString(Clipboard clip, String writeMe) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
} }
由於拷貝String,jdk中已經有類StringSelection支持,如果要拷貝一個文件,或者目錄,則要參考此類,對其進行修改,類名:FileSelection.java (改的可能不是很成功,但能夠用)
內容為:
/* @(#)FileSelection.java 07/05/14
* @author me
* Copy file from FoxEDM,Paste to System(e.g:2000 or xp etc)
*/
package com.foxera.foxlink.ui;import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.*;
import java.util.List;
import java.util.ArrayList;
/**
* A <code>Transferable</code> which implements the capability required
* to transfer a <code>String</code>.
*
* This <code>Transferable</code> properly supports
* <code>DataFlavor.javaFileListFlavor</code>
* and all equivalent flavors. Support for
* <code>DataFlavor.plainTextFlavor</code>
* and all equivalent flavors is <b>deprecated</b>. No other
* <code>DataFlavor</code>s are supported.
*
* @see java.awt.datatransfer.DataFlavor#javaFileListFlavor
* @see java.awt.datatransfer.DataFlavor#plainTextFlavor
*/
public class FileSelection implements Transferable, ClipboardOwner { private static final int FILE= 0;
private static final int PLAIN_FILE = 1; private static final DataFlavor[] flavors = {
DataFlavor.javaFileListFlavor,
DataFlavor.javaFileListFlavor
}; private List data;
/**
* Creates a <code>Transferable</code> capable of transferring
* the specified <code>String</code>.
*/
public FileSelection(List data) {
this.data = data;
} /**
* Returns an array of flavors in which this <code>Transferable</code>
* can provide the data. <code>DataFlavor.javaFileListFlavor</code>
* is properly supported.
* Support for <code>DataFlavor.plainTextFlavor</code> is
* <b>deprecated</b>.
*
* @return an array of length two, whose elements are <code>DataFlavor.
* stringFlavor</code> and <code>DataFlavor.plainTextFlavor</code>
*/
public DataFlavor[] getTransferDataFlavors() {
// returning flavors itself would allow client code to modify
// our internal behavior
return (DataFlavor[])flavors.clone();
} /**
* Returns whether the requested flavor is supported by this
* <code>Transferable</code>.
*
* @param flavor the requested flavor for the data
* @return true if <code>flavor</code> is equal to
* <code>DataFlavor.javaFileListFlavor</code> or
* <code>DataFlavor.plainTextFlavor</code>; false if <code>flavor</code>
* is not one of the above flavors
* @throws NullPointerException if flavor is <code>null</code>
*/
public boolean isDataFlavorSupported(DataFlavor flavor) {
// JCK Test StringSelection0003: if 'flavor' is null, throw NPE
for (int i = 0; i < flavors.length; i++) {
if (flavor.equals(flavors[i])) {
return true;
}
}
return false;
} /**
* Returns the <code>Transferable</code>'s data in the requested
* <code>DataFlavor</code> if possible. If the desired flavor is
* <code>DataFlavor.javaFileListFlavor</code>, or an equivalent flavor,
* the <code>String</code> representing the selection is
* returned. If the desired flavor is
* <code>DataFlavor.plainTextFlavor</code>,
* or an equivalent flavor, a <code>Reader</code> is returned.
* <b>Note:</b> The behavior of this method for
* </code>DataFlavor.plainTextFlavor</code>
* and equivalent <code>DataFlavor</code>s is inconsistent with the
* definition of <code>DataFlavor.plainTextFlavor</code>.
*
* @param flavor the requested flavor for the data
* @return the data in the requested flavor, as outlined above
* @throws UnsupportedFlavorException if the requested data flavor is
* not equivalent to either <code>DataFlavor.stringFlavor</code>
* or <code>DataFlavor.plainTextFlavor</code>
* @throws IOException if an IOException occurs while retrieving the data.
* By default, StringSelection never throws this exception, but a
* subclass may.
* @throws NullPointerException if flavor is <code>null</code>
* @see java.util.ArrayList<E>
*/
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException
{
if (flavor.equals(flavors[FILE])) {
return (Object)data;
} else if (flavor.equals(flavors[PLAIN_FILE])) {
return new ArrayList<File>(data == null ? null : data);
} else {
throw new UnsupportedFlavorException(flavor);
}
} public void lostOwnership(Clipboard clipboard, Transferable contents) {
}}測試代碼為:
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.util.List;
import java.io.File;
import java.util.ArrayList;
public class WriteFile
{
public static void main(String[] args)
{
Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
File file = new File("c:\\1.txt");//目錄也可以
List<File> listFile = new ArrayList<File>();
listFile.add(file);
setClipboardFile(sysc,listFile);
} protected static void setClipboardFile(Clipboard clip, List writeMe) {
Transferable tFile = new FileSelection(writeMe);
clip.setContents(tFile, null);
} }從操作系統的剪貼板中讀取資料,可以參考:
protected static String getClipboardContent(Clipboard clip) throws Exception{
//get content from clipboard
Transferable clipT = clip.getContents(null);
if (clipT != null) {
// check content's type
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
{
return (String)clipT.getTransferData(DataFlavor.stringFlavor);
}
else if(clipT.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
{
Object ob = clipT.getTransferData(DataFlavor.javaFileListFlavor);
List fileList = (List)ob;
if((fileList != null)&&(fileList.size() > 0))
{
for(int k=0;k<fileList.size();k++)
{
System.out.println(((File)fileList.get(k)).getAbsolutePath());
//根據路徑拷貝文件
}
}
}
}
return null;
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.WritableFont;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
public class WriteExcle {
public static void writeExcel(String sourcefile,String targetfile) throws Exception {
jxl.Workbook rw = jxl.Workbook.getWorkbook(new File(sourcefile));
//创建可写入的Excel工作薄对象
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile), rw);
//读取第一张工作表
jxl.write.WritableSheet ws = wwb.getSheet(0);
//获得第一个单元格对象
jxl.write.WritableCell wc = ws.getWritableCell(0, 0);
//判断单元格的类型, 做出相应的转化
if (wc.getType()==CellType.EMPTY)
{
System.out.println("runing");
Label lp = new Label(2,2,"modify the empty");
ws.addCell(lp);
}
//写入Excel对象
wwb.write();
//关闭可写入的Excel对象
wwb.close();
//关闭只读的Excel对象
rw.close(); }
public static void main(String[] args) {
try{
File f = new File("c:\\kk.xls");
writeExcel( "c:\\kk.xls","c:\\kk11.xls");
}
catch(Exception e){
System.out.println(e.toString());
}
}
}
poi 操作excleimport java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;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.poifs.filesystem.POIFSFileSystem;
public class POIExcel {
public static void main(String args[])
{
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("c:\\workbook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short)0);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("c:\\workbook.xls");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}