商业数据分析(对Excel的处理)
需求:对一个Excel表,其中一列数据是orders,这列数据是有重复的,这一列数据分别对应另外一列不同的price;另外一列数据也是orders,这一列orders是不重复的,这一列的orders分别对应不同的phoneID,phoneID是有重复的,现在需要找出phoneID和price的对应关系,其实也就是要找出每一个用户的消费总金额。思路:维护三个map,map1存的是第一列的order
·
需求:对一个Excel表,其中一列数据是orders,这列数据是有重复的,这一列数据分别对应另外一列不同的price;另外一列数据也是orders,这一列orders是不重复的,这一列的orders分别对应不同的phoneID,phoneID是有重复的,现在需要找出phoneID和price的对应关系,其实也就是要找出每一个用户的消费总金额。
思路:维护三个map,map1存的是第一列的orders和price数据;map2存的是第二列的orders和phoneID对应的数据。先对map1的orders处理,找出每一个order对应的price数据之和。再对map2的orders进行处理,找出每一个phoneID对应哪些order,再从map1中找对应order的price。最后把phoneID和price作为一对键值对存在map3中。
代码:
package ExcelImplement;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ExcelImplement {
public void readExcel(File file) throws BiffException, IOException{
try {
InputStream is = new FileInputStream(file.getAbsolutePath());
Workbook wb = Workbook.getWorkbook(is);
int sheet_size = wb.getNumberOfSheets();
for(int i = 0 ; i < sheet_size ; i++){
Sheet sheet = wb.getSheet(i);
int rows = sheet.getRows();
System.out.println();
int j = 1 ;
//m,n分别作为游标指示当前走到的位置
int m = 1;
int n = 2;
List<Double> list = new ArrayList<>();
double count = 0;
List<Integer> list2 = new ArrayList<>();
Map<Integer, Double> map = new HashMap<Integer, Double>();
while(j < rows -1){
//如果相邻的两行数据是相等的话,继续遍历
while( (sheet.getCell(1, m).getContents()).equals(sheet.getCell(1, n).getContents()) && n < rows-1){
count += Double.parseDouble(sheet.getCell(4, m).getContents());
m++;
n++;
}
count += Double.parseDouble(sheet.getCell(4, m).getContents());
list2.add(Integer.parseInt(sheet.getCell(1, m).getContents()));
list.add(count);
map.put(Integer.parseInt(sheet.getCell(1, m).getContents()), count);
j = n ;
m++;
n++;
count=0;
}
Map<Integer, Integer> map2 = new HashMap<>();
Map<Integer, Double> map3 = new HashMap<>();
for(int c = 1 ; c < rows ; c++){
if (""==sheet.getCell(5, c).getContents()) {
continue;
}
map2.put(Integer.parseInt(sheet.getCell(5, c).getContents()),Integer.parseInt(sheet.getCell(6,c).getContents()));
}
//分别遍历map2和map,如果是符合的,就提出加进map3中。
for(Integer key:map2.keySet()){
int phoneTemp = map2.get(key);
double tempAll=0.0;
for(Integer key1:map2.keySet()){
if (map2.get(key1)==phoneTemp) {
if (null==map.get(key1)) {
continue;
}
tempAll+=map.get(key1);
}
}
map3.put(phoneTemp, tempAll);
}
File data = new File("D://data.txt");
PrintWriter pWriter = new PrintWriter(data);
for(Integer key:map3.keySet()){
pWriter.write(key+" "+map3.get(key)+"\n");
System.out.println("phone:"+key+"\t total:"+map3.get(key));
}
pWriter.close();
// int sum = 0;
// ArrayList <Integer> list5 = new ArrayList<>();
// for(Integer key:map2.keySet()){
// int key1 = key;
// int key2 = key+1;
// while(map2.get(key1) == map2.get(key2)){
// sum += map.get(key1);
// key1++;
// key2++;
// }
// sum += map.get(key1);
// list5.add(sum);
// key1++;
// key2++;
// sum = 0;
// }
// for (Integer double1 : list5) {
// System.out.println(double1);
// }
// for(Integer key:map2.keySet()){
// System.out.println(key+":"+map2.get(key));
// }
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws BiffException, IOException {
ExcelImplement ei = new ExcelImplement();
File file = new File("D://info2.xls");
ei.readExcel(file);
}
public static String[] convertToStringArray(ArrayList<String> list) {
String[] covertedArray = new String[list.size()];
covertedArray = (String[]) list.toArray(covertedArray);
return covertedArray;
}
}
需要导入对Excel处理的jxl.jar包。
推荐内容
点击阅读全文
更多推荐
所有评论(0)