Commit 529867a9 authored by 竹天卫's avatar 竹天卫

word 模板导出

parent b0deefc5
......@@ -21,6 +21,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
......@@ -843,6 +844,18 @@ public class EntrustController {
}
@ApiOperation("测试xml转word")
@PostMapping("/testXml2Word")
public void testXml2Word(MultipartFile file, HttpServletResponse response) {
try {
entrustService.testXml2Word(file, response);
} catch (Exception e) {
log.debug("测试xml转word{}", e);
}
}
}
......
......@@ -6,6 +6,7 @@ import cn.wise.sc.cement.business.model.query.*;
import cn.wise.sc.cement.business.model.vo.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
......@@ -178,6 +179,6 @@ public interface IEntrustService extends IService<Entrust> {
String fileName, HttpServletResponse response);
void testXml2Word(MultipartFile file, HttpServletResponse response);
}
......@@ -37,9 +37,12 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
......@@ -6332,4 +6335,28 @@ public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> impl
}
}
/**
* 测试xml转word
* @param file
* @param response
*/
@Override
public void testXml2Word(MultipartFile file, HttpServletResponse response){
try{
InputStream in = file.getInputStream();
ExcelUtil.readData((FileInputStream)in);
} catch (Exception e) {
e.printStackTrace();
}
}
}
......@@ -20,13 +20,9 @@ import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
/**
* excel工具类
......@@ -459,6 +455,68 @@ public class ExcelUtil {
return wb;
}
/**
* 读取文件
* @param fis
* @return
* @throws Exception
*/
@SuppressWarnings("resource")
public static List<Map<Integer, String>> readData(FileInputStream fis) throws Exception {
XSSFWorkbook hwb = new XSSFWorkbook(fis);
List<Map<Integer, String>> usermap = new ArrayList<>();
// 获取第一个sheet页
XSSFSheet sheetAt = hwb.getSheetAt(0);
if (sheetAt == null) {
return usermap;
}
// 遍历行里面的单元格内容.
for (int i = 1; i <= sheetAt.getLastRowNum(); i++) {
// User user = new User();
Map<Integer, String> map = new HashMap<Integer, String>();
// 得到每一行
XSSFRow row = sheetAt.getRow(i);
// 如果为空就跳出
if (row == null) {
continue;
}
// 遍历列
for (int j = 0; j < row.getLastCellNum(); j++) {
// 遍历列里面的内容
XSSFCell cell = row.getCell(j);
if (cell == null) {
map.put(j, null);
}
try {
map.put(j, cell.getStringCellValue());
} catch (Exception e) {
map.put(j, new DecimalFormat("#0").format(cell.getNumericCellValue()).toString());
}
}
usermap.add(map);
}
return usermap;
}
public static void main(String[] args) throws IOException {
//指定数据存放的位置
OutputStream outputStream = new FileOutputStream("D:\\test.xlsx");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment