写excel文件

package excel;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;

public class ExcelFile {

public static List<Integer> findEquals(int[] result, int value) {
List<Integer> bigList = new ArrayList<Integer>();
for (int i = 0; i < result.length; i++) {
if (result[i] == value) {
bigList.add(i + 1);
}
}
return bigList;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream out = null;
FileReader in = null;
BufferedReader br = null;
List<String> content = new ArrayList<String>();
try {
out = new FileOutputStream("result.xls");
in = new FileReader("1.txt");
br = new BufferedReader(in);
String line = br.readLine();
while (line != null) {
content.add(line);
line = br.readLine();
}
int[] result = new int[30];
for (int i = content.size() - 1; i >= 0; i--) {

String[] lines = content.get(i).split(" ");
for (int j = 1; j < lines.length; j++) {
result[Integer.parseInt(lines[j]) - 1]++;
}
int[] sort = Arrays.copyOf(result, result.length);
Arrays.sort(sort);
HSSFSheet sheet = wb.createSheet(lines[0]);
for (int j = 0; j < result.length; j++) {
HSSFRow row = sheet.createRow(j);
HSSFCell cell = row.createCell(0);
cell.setCellValue(result[j]);
}

Map<Integer, Integer> tmpHash = new HashMap<Integer, Integer>();
if (i - 1 >= 0) {
String[] nextlines = content.get(i - 1).split(" ");
for (int j = 1; j < nextlines.length; j++) {
Integer key = Integer.parseInt(nextlines[j]);
if (tmpHash.get(result[key - 1]) != null) {
Integer value = tmpHash.get(result[key - 1]);
value = value + 1;
tmpHash.put(result[key - 1], value);
} else {
tmpHash.put(result[key - 1], 1);
}
}
}

for (int j = sort[0]; j <= sort[sort.length - 1]; j++) {
HSSFRow row = sheet.createRow(j + 39 - sort[0]);
HSSFCell cell = row.createCell(0);
HSSFCell formal = row.createCell(1);
cell.setCellValue(j);
formal.setCellFormula("COUNTIF(A1:A30,A" + (j + 40 - sort[0]) + ")");
if (tmpHash.get(j) != null) {
HSSFCell num = row.createCell(2);
num.setCellValue(tmpHash.get(j));
}
List tmp = findEquals(result,j);
HSSFCell numcell = row.createCell(3);
numcell.setCellValue(tmp.toString());
}

CellRangeAddress region = new CellRangeAddress(39, 45, 5, 8);
sheet.addMergedRegion(region);
HSSFRow row = sheet.getRow(39);
HSSFCell cell = row.createCell(5);

List<Integer> big = new ArrayList<Integer>();
for (int j = sort.length - 1; j > sort.length - 7; j--) {
List<Integer> bigtmp = findEquals(result, sort[j]);
for (Integer k : bigtmp) {
if (!big.contains(k))
big.add(k);
}
if (big.size() > 7) {
break;
}
}
StringBuffer tmp = new StringBuffer();
for (int j : big) {
tmp.append(j + " ");
}
tmp.append(" little:");
List<Integer> little = new ArrayList<Integer>();

for (int j = 0; j < 7; j++) {
List<Integer> littletmp = findEquals(result, sort[j]);
for (Integer k : littletmp) {
if (!little.contains(k))
little.add(k);
}
if (little.size() > 7) {
break;
}
}
for (int j : little) {
tmp.append(j + " ");
}
cell.setCellValue("big:" + tmp.toString());

Font font = wb.createFont();
// 设置字体样式
font.setFontHeightInPoints((short) 12);
font.setFontName("Courier New");
font.setBold(true);
HSSFCellStyle style = wb.createCellStyle(); // 样式对象
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style.setFont(font);
style.setWrapText(true);
cell.setCellStyle(style);
sheet.autoSizeColumn(3);

}

wb.write(out);
System.out.println("success!");
} catch (Exception e) {
e.printStackTrace();

} finally {
try {
out.close();
in.close();
br.close();
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

原文地址:https://www.cnblogs.com/rspb/p/7218545.html