需要apache的poi包
import 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.HSSFCellStyle;
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.poifs.filesystem.POIFSFileSystem;
public class test {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream("c:/hpsg.xls");
POIFSFileSystem fs = new POIFSFileSystem(file);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet1 = wb.getSheet("Sheet1");
HSSFRow row = sheet1.createRow(6);
HSSFCell cell = row.createCell((short)1);
HSSFRichTextString hrts = new HSSFRichTextString("hello");
cell.setCellValue(hrts);
HSSFCellStyle hSSFCellStyle = wb.createCellStyle();
// 给excel单元格加边框
hSSFCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
hSSFCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
hSSFCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
hSSFCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cell.setCellStyle(hSSFCellStyle);
FileOutputStream fileOut = new FileOutputStream("c:/hpsg.xls");
wb.write(fileOut);
fileOut.close();
// 本地打开文件的命令
Runtime.getRuntime().exec("cmd /c start c:/hpsg.xls");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}