当前位置: 动力学知识库 > 问答 > 编程问答 >

How to write something to MS Excel / Word wih Java?

问题描述:

I need to export some data from my Java app to Excel or Word file. It can be tables, bars, graphs... So, what is the best way to do this? In C# I used to do it via the standard library, but I don't know how to do this in Java.

Oh, and I also wanted to ask about DCOM interface. Can I use it somehow without running Excel?

网友答案:

You can have a look at the following libraries

http://poi.apache.org/

http://jexcelapi.sourceforge.net/

网友答案:

The easiest way to export data to excel is using csv format.

Here you have an example from mkyong.com/java/how-to-export-data-to-csv-file-java:

package com.mkyong.test;

import java.io.FileWriter;
import java.io.IOException;

public class GenerateCsv
{
   public static void main(String [] args)
   {
       generateCsvFile("c:\\test.csv"); 
   }

   private static void generateCsvFile(String sFileName)
   {
    try
    {
        FileWriter writer = new FileWriter(sFileName);

        writer.append("DisplayName");
        writer.append(',');
        writer.append("Age");
        writer.append('\n');

        writer.append("MKYONG");
        writer.append(',');
        writer.append("26");
            writer.append('\n');

        writer.append("YOUR NAME");
        writer.append(',');
        writer.append("29");
        writer.append('\n');

        //generate whatever data you want

        writer.flush();
        writer.close();
    }
    catch(IOException e)
    {
         e.printStackTrace();
    } 
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: