189 8069 5689

写excel文件-创新互联

package excel;写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;

成都创新互联主营双清网站建设的网络公司,主营网站建设方案,成都app软件开发公司,双清h5小程序制作搭建,双清网站营销推广欢迎双清等地区企业咨询

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 findEquals(int[] result, int value) {
List bigList = new ArrayList();
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 content = new ArrayList();
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 tmpHash = new HashMap();
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 big = new ArrayList();
for (int j = sort.length - 1; j > sort.length - 7; j--) {
List 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 little = new ArrayList();

for (int j = 0; j < 7; j++) {
List 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();
}
}
}

}


新闻名称:写excel文件-创新互联
地址分享:http://cdxtjz.cn/article/cohgcc.html

其他资讯