Apache POI library excel file, Mat in OpenCV to Excel file
/ 아파치 POI 라이브러리 엑셀파일 쓰기 OpenCV Mat 에서 엑셀파일 만들기 (java, c++)
Java 에서 OpenCV 를 이용하여 Mat data를 만들었을 때 POI로 엑셀 저장을 하기 위한 메소드
Method for saving Mat data in Java OpenCV using POI
설명
저장할 excel 파일 경로+이름(absolutePath) 과, 저장할 Mat 을 입력받는다.
Mat 의 channel 은 sheet
rows 는 row
cols 는 cell (엑셀에서)
저장 잘된다.
Description
Input absolutePath of file we wanna save (path + name)
Mat channel -> sheet
rows -> row
cols -> cell (in excel)
Saved well
Code.. lemme see code!!!!!!
코드.. 코드를 보자!!!!!!!!
{
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
class~
}
public static void saveMat2ExcelData(String absPath, Mat mat) {
int countSheet = mat.channels();
int countRow = mat.rows();
int countCell = mat.cols();
XSSFWorkbook workbook = new XSSFWorkbook();
String nameSheet;
for (int k = 0; k < countSheet; ++k)
{
nameSheet = "Sheet ";
nameSheet += Integer.toString(k+1);
XSSFSheet sheet = workbook.createSheet(nameSheet);
for (int i = 0; i < countRow; ++i)
{
XSSFRow row = sheet.createRow(i);
for (int j = 0; j < countCell; ++j)
{
XSSFCell cell = row.createCell(j);
cell.setCellValue(mat.get(i, j)[k]);
}
}
}
try {
if (!new File(absPath).exists())
{
new File(absPath);
}
FileOutputStream outputStream = new FileOutputStream(absPath);
workbook.write(outputStream);
workbook.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}