生成Excel文件并导出
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
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;
public class Demo {
public static void main(String[] args) {
writeDbtoExcel();
}
public static String writeDbtoExcel(){
Connection con = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/student","sa","admin");
} catch (Exception e) {
e.printStackTrace();
}
String path="D:/测试生成Excel表.xls";
HSSFWorkbook book=new HSSFWorkbook();
HSSFSheet sheet=book.createSheet("测试");
String sql="select distinct Student.number,Student.name,Student.phone,Student.status from Student";
generateExcel(con,sheet,sql);
try {
FileOutputStream out=new FileOutputStream(path);
book.write(out);
book.close();
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
public static String generateExcel(Connection con, HSSFSheet sheet, String sql){
try {
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(sql);
ResultSetMetaData rsmd=rs.getMetaData();
int c=rsmd.getColumnCount();
HSSFRow row0=sheet.createRow(0);
for(int i=0;i<c;i++){
HSSFCell cel=row0.createCell(i);
cel.setCellValue(rsmd.getColumnName(i+1));
}
int r=1;
while(rs.next()){
HSSFRow row=sheet.createRow(r++);
for(int i=0;i<c;i++){
HSSFCell cel=row.createCell(i);
cel.setCellValue(rs.getString(i+1));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sql;
}
}
Java将指定文件夹中的内容复制到新的文件夹中
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.io.OutputStream;
public class TestCopy {
public static void main(String[] args) {
copy("D:/a", "D:/b");
System.out.println("共移动文件数目:" + size);
}
public static void copy(String sourcePath, String newPath) {
File file = new File(sourcePath);
if (file != null && file.exists()) {
String name = newPath + "/" + sourcePath.substring(sourcePath.lastIndexOf("/") + 1, sourcePath.length());
File dir = new File(name);
if (!dir.exists()) {
dir.mkdir();
}
copyDir(sourcePath, name);
} else {
return;
}
}
private static int size;
private static void copyDir(String sourcePath, String newPath) {
File sourceFile = new File(sourcePath);
if (sourceFile.exists() && sourceFile != null) {
if (sourceFile.isFile()) {
copyFile(sourcePath, newPath);
System.out.println(sourcePath + " ---> " + newPath);
size++;
} else if (sourceFile.isDirectory()) {
File dir = new File(newPath);
if (!dir.exists()) {
dir.mkdir();
}
for (File con : sourceFile.listFiles()) {
copyDir(sourcePath + "/" + con.getName(), newPath + "/" + con.getName());
}
}
} else {
return;
}
}
private static void copyFile(String soucePath, String newPath) {
File sourceFile = new File(soucePath);
File newFile = new File(newPath);
InputStream fin = null;
OutputStream fout = null;
try {
fin = new FileInputStream(sourceFile);
fout = new FileOutputStream(newFile);
byte[] flush = new byte[1024];
int len = -1;
while ((len = fin.read(flush)) != -1) {
fout.write(flush, 0, len);
}
fout.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
编写zip的工具类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtilss {
private static final int BUFFER_SIZE = 2 * 1024;
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException{
long start = System.currentTimeMillis();
ZipOutputStream zos = null ;
try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils",e);
}finally{
if(zos != null){
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null ;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils",e);
}finally{
if(zos != null){
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception{
byte[] buf = new byte[BUFFER_SIZE];
if(sourceFile.isFile()){
zos.putNextEntry(new ZipEntry(name));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourceFile.listFiles();
if(listFiles == null || listFiles.length == 0){
if(KeepDirStructure){
zos.putNextEntry(new ZipEntry(name + "/"));
zos.closeEntry();
}
}else {
for (File file : listFiles) {
if (KeepDirStructure) {
compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
} else {
compress(file, zos, file.getName(),KeepDirStructure);
}
}
}
}
}
public static void main(String[] args) throws Exception {
FileOutputStream fos1 = new FileOutputStream(new File("D:/mytest01.zip"));
ZipUtilss.toZip("D:/log", fos1,true);
List<File> fileList = new ArrayList<>();
fileList.add(new File("D:/tool-java/Java/jdk1.8.0_111/bin/jar.exe"));
fileList.add(new File("D:/tool-java/Java/jdk1.8.0_111/bin/java.exe"));
FileOutputStream fos2 = new FileOutputStream(new File("D:/mytest02 .zip"));
ZipUtilss.toZip(fileList, fos2);
}
}