导出到用户桌面的小方法:
FileSystemView fsv = FileSystemView.getFileSystemView();
fsv.getHomeDirectory() +"\\" +"你想要的名字.xls"
首先要在pom.xml中导入poi依赖
import java.io.FileOutputStream;
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.ss.util.CellRangeAddress;
/**
* @author 街有三旬
*/
public class Test {
public static void main(String[] args) {
//web对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建表头等
HSSFSheet sheet = wb.createSheet("学生报名信息");
//创建行
HSSFRow row = sheet.createRow(0);
HSSFRow row1 = sheet.createRow(1);
//创建列
HSSFCell cell = row.createCell(0);
HSSFCell cell2 = row1.createCell(0);
//设置居中格式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell2.setCellValue("这个是什么值啊????????????????????????");
cell.setCellValue("我是写入的值");
cell.setCellStyle(style); .--------> 将cell单元格设为居中
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 4));
解释 -----> CellRangeAddress 合并单元格 1 从第二行开始 1 到第二行结束 0 从第一个单元格 4 到第五个单元格结束
try {
FileOutputStream output = new FileOutputStream("E:\\学生报名信息.xls");
wb.write(output);
output.flush();
System.out.println("成功创建excel文件");
} catch (Exception e) {
e.printStackTrace();
}
}
}
截取字符串需要根据多个分隔符只要加 | ,就可以了
String[] strs1=substring.toString().split("=|,")
未完待续