Creating a CSV File in java from a HashMap

前端 未结 2 1569
谎友^
谎友^ 2021-01-25 02:54

I have a hashMap in java in terms of some keys which each key is indicating a flow. Then each value showing statics about each packet belongs to that flow.

What I need t

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 03:44

    If you really want an Excel file, the best library for creating one is Andy Khan's JExcel.

    I think you'd need one worksheet per flow, with .csv pairs for each one, sorted by time.

    If these are graphs of a variable versus time, wouldn't "time" be the first value in each pair?

    Here's how I'd do it. It works perfectly for the simple test case I supplied - it's working code that you'll be able to extend.

    package jexcel;
    
    import jxl.Workbook;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class JExcelUtils
    {
        public static void main(String[] args)
        {
            String fileName = ((args.length > 0) ? args[0] : "jexcel.xls");
            Map> csv = new HashMap>()
            {{
                put("Flow1", fromArrayToList(new double[][]
                {
                    { 0.0, 0.0 },
                    { 0.1, 1.0 },
                    { 0.2, 2.0 },
                    { 0.3, 3.0 },
                    { 0.4, 4.0 },
                    { 0.5, 5.0 },    
                }));
                put("Flow2", fromArrayToList(new double[][]
                {
                    { 0.0, 0.0 },
                    { 0.1, 1.0 },
                    { 0.2, 4.0 },
                    { 0.3, 9.0 },
                    { 0.4, 16.0 },
                    { 0.5, 25.0 },
                }));
            }};
            WritableWorkbook excelContents = null;
    
            try
            {
                File excelFile = new File(fileName);
                excelContents = createExcel(excelFile, csv);
                excelContents.write();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (WriteException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try { if (excelContents != null) excelContents.close(); } catch (Exception e) { e.printStackTrace(); }
            }
        }
    
    
        public static List fromArrayToList(double [][] input)
        {
            List result = new ArrayList();
    
            for (int i = 0; i < input.length; ++i)
            {
                result.add(new Pair(input[i][0], input[i][1]));            
            }
    
            return result;
        }
    
        public static WritableWorkbook createExcel(File excelFile, Map> worksheets) throws IOException, WriteException
        {
            WritableWorkbook result = Workbook.createWorkbook(excelFile);
    
            int order = 0;
            for (String worksheetName : worksheets.keySet())
            {
                WritableSheet worksheet = result.createSheet(worksheetName, order++);
                List worksheetValues = worksheets.get(worksheetName);
                for (int row = 0; row < worksheetValues.size(); ++row)
                {
                    worksheet.addCell(new jxl.write.Number(0, row, worksheetValues.get(row).getX()));
                    worksheet.addCell(new jxl.write.Number(1, row, worksheetValues.get(row).getY()));
                }
            }
    
            return result;
        }
    
    }
    
    
    class Pair
    {
        private double x;
        private double y;
    
        Pair(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
    
        public double getX()
        {
            return x;
        }
    
        public double getY()
        {
            return y;
        }
    }
    

提交回复
热议问题