package xiugai;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
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.ss.usermodel.RichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XiuGai {
public static void XiuGai(String filepath) throws Exception {
File file = new File(filepath);
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
try {
if (!file.isDirectory()) {
} else if (file.isDirectory()) {
if (!readfile.isDirectory()) {
// System.out.println("2name=" + readfile.getName());
System.out.println("3name=" + readfile.getName());
String name = readfile.getName();
System.out.println(name);
InputStream is = new FileInputStream(
"C:/Users/adnr/Desktop/哈密公路管理局03版excel/桥梁/小桥/巴里坤公路管理分局/巴里坤公路管理分局小桥excel/"
+ name);
// C:\Users\adnr\Desktop\哈密公路管理局03版excel\桥梁\小桥\巴里坤公路管理分局\巴里坤公路管理分局小桥excel
HSSFWorkbook demoWorkbook1 = new HSSFWorkbook(is);
HSSFSheet sheet = demoWorkbook1.getSheetAt(0);
// XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
// XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
String XXX = "";
String code = readfile.getName().substring(0, readfile.getName().lastIndexOf("."));// 桥梁编码
System.out.println(code);
String orgname = sheet.getRow(19).getCell(4).getStringCellValue();// 养护单位
String bridgeName = sheet.getRow(2).getCell(1).getStringCellValue();
String routeName = sheet.getRow(13).getCell(4).getStringCellValue();
String routeCode = sheet.getRow(13).getCell(10).getStringCellValue();
String type = sheet.getRow(16).getCell(4).getStringCellValue();
String pot = sheet.getRow(16).getCell(10).getStringCellValue();
String yht = sheet.getRow(19).getCell(10).getStringCellValue();
String gl = sheet.getRow(22).getCell(4).getStringCellValue();
String glt = sheet.getRow(22).getCell(10).getStringCellValue();
String jg = sheet.getRow(25).getCell(4).getStringCellValue();
String jgt = sheet.getRow(25).getCell(10).getStringCellValue();
XXX = code + "!!" + orgname;
FileOutputStream out1 = null;
if ("巴里坤公路管理分局".equals(orgname)) {
System.out.println(XXX + "这个是正确的格式!");
} else {
String x = "巴里坤公路管理分局";
InputStream that = new FileInputStream(
"C:/Users/adnr/Desktop/哈密公路管理局03版excel/桥梁/小桥/巴里坤公路管理分局/"
+ name);
HSSFWorkbook demoWorkbook2 = new HSSFWorkbook(that);
HSSFSheet demo = demoWorkbook2.getSheetAt(0);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
demo.getRow(2).getCell(1).setCellValue(bridgeName);
demo.getRow(13).getCell(4).setCellValue(routeName);
demo.getRow(13).getCell(10).setCellValue(routeCode);
demo.getRow(16).getCell(4).setCellValue(type);
demo.getRow(16).getCell(10).setCellValue(pot);
demo.getRow(19).getCell(4).setCellValue(x);
demo.getRow(19).getCell(10).setCellValue(yht);
demo.getRow(22).getCell(4).setCellValue(gl);
demo.getRow(22).getCell(10).setCellValue(glt);
demo.getRow(25).getCell(4).setCellValue(jg);
demo.getRow(25).getCell(10).setCellValue(jgt);
File imageFile = new File(
"C:/Users/adnr/Desktop/哈密公路管理局03版数据/桥梁/小桥/巴里坤公路管理分局/巴里坤公路管理分局小桥二维码/" + code + ".png");
BufferedImage bufferImg = ImageIO.read(imageFile);
ImageIO.write(bufferImg, "png", byteArrayOut);
HSSFPatriarch patriarch = demo.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 22, 0, 250,
(short) 10, 2, (short) 13, 12);// 126, 16
//插入图片
anchor.setAnchorType(3);
patriarch.createPicture(anchor, demoWorkbook2.addPicture(
byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
System.out.println("第" + i + "个表格:" + code
+ "成功移动到伊吾公路管理分局文件夹下");
out1 = new FileOutputStream(
"C:/Users/adnr/Desktop/哈密公路管理局03版excel/桥梁/小桥/巴里坤公路管理分局/巴里坤公路管理分局小桥excel/1/"
+ code + ".xls");
}
} else if (readfile.isDirectory()) {
XiuGai(filepath + "\\" + filelist[i]);
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
}
}
public static <HttpServletResponse> void main(String[] args)
throws Exception {
XiuGai xg = new XiuGai();
try {
XiuGai("C:/Users/adnr/Desktop/哈密公路管理局03版excel/桥梁/小桥/巴里坤公路管理分局/巴里坤公路管理分局小桥excel/");
// xg.a();
// file.getName();
} catch (Exception ex) {
}
}
}求大神指点是哪里错了
添加回答
举报
0/150
提交
取消