日期:2014-05-20 浏览次数:20839 次
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Cell;
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.ss.usermodel.WorkbookFactory;
public class OperExcel {
public static void main(String[] args) throws Exception {
InputStream inp = new FileInputStream("d://workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("d://workbook.xlsx");
wb.write(fileOut);
fileOut.close();
inp.close();
}
public static void main(String[] args) {
// 解压Book1.xlsx
ZipFile xlsxFile;
try {
xlsxFile = new ZipFile(new File("d:\\12.xlsx"));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 先读取sharedStrings.xml这个文件备用
ZipEntry sharedStringXML = xlsxFile.getEntry("xl/sharedStrings.xml");
InputStream sharedStringXMLIS = xlsxFile.getInputStream(sharedStringXML);
Document sharedString;
sharedString = dbf.newDocumentBuilder().parse(sharedStringXMLIS);
NodeList str = sharedString.getElementsByTagName("t");
String sharedStrings[] = new String[str.getLength()];
for (int n = 0; n < str.getLength(); n++) {