excel 合并单元格测试导入

import com.skytech.egov.questions.model.TAnswers;
import com.skytech.egov.questions.model.TQuestions;
import com.skytech.egov.questions.service.QuestionsService;
import com.skytech.egov.questions.util.AnalysisDocumentUtil;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
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.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

@Component
public class AnalysisDocumentUtil {
  @Resource
  private QuestionsService questionsService;
  
  private static String firstRowValue = "";
  
  private static String uuid;
  
  private static List<TQuestions> questionsList = new ArrayList<>();
  
  private static List<TAnswers> answerList = new ArrayList<>();
  
  public void documentUtil(int questionsId) throws Exception {
    FileInputStream fis = new FileInputStream("C:/Users/OFFICE/Desktop/test.xls");
    XSSFWorkbook xSSFWorkbook = new XSSFWorkbook(fis);
    Sheet sheet = xSSFWorkbook.getSheetAt(0);
    for (int i = 0; i <= sheet.getLastRowNum(); i++) {
      String rowStr = "";
      Row row = sheet.getRow(i);
      TAnswers answers = new TAnswers();
      for (Cell cell : row) {
        String value = getValue(sheet, i, cell.getColumnIndex());
        if ("".equals(value)) {
          value = cell.toString();
          if (cell.getColumnIndex() == 1) {
            answers.setOptionDesc(value);
          } else if (cell.getColumnIndex() == 2) {
            int i1 = (new Double(Double.parseDouble(value))).intValue();
            answers.setScore((new Long(i1)).longValue());
            answers.setSubjectId(uuid);
          } else if (cell.getColumnIndex() == 3) {
            answerList.add(answers);
          } 
        } 
        rowStr = rowStr + "--" + value;
      } 
      System.out.println(rowStr);
    } 
    this.questionsService.saveQuestionsAndAnswers(questionsList, answerList, questionsId);
    System.out.println("success");
  }
  
  private static String getValue(Sheet sheet, int row, int column) {
    int sheetMergeCount = sheet.getNumMergedRegions();
    for (int i = 0; i < sheetMergeCount; i++) {
      CellRangeAddress range = sheet.getMergedRegion(i);
      int firstColumn = range.getFirstColumn();
      int lastColumn = range.getLastColumn();
      int firstRow = range.getFirstRow();
      int lastRow = range.getLastRow();
      if (row >= firstRow && row <= lastRow && column >= firstColumn && column <= lastColumn) {
        Row fRow = sheet.getRow(firstRow);
        Cell fCell = fRow.getCell(firstColumn);
        if (!fCell.getStringCellValue().equals(firstRowValue)) {
          uuid = UUID.randomUUID().toString();
          String stringCellValue = fCell.getStringCellValue();
          TQuestions questions = new TQuestions();
          questions.setSubjectDesc(stringCellValue);
          questions.setGuid(uuid);
          questionsList.add(questions);
          firstRowValue = fCell.getStringCellValue();
        } 
        return fCell.getStringCellValue();
      } 
    } 
    return "";
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值