在Java中生成excel文件作为输出

时间:2014-02-16 22:49:55

标签: java excel

以下java代码:

import java.io.File;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


class extract
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String testData = new Scanner( new File("109.txt") ).useDelimiter("\\A").next();
        //String testData = "#1|77|1391436891|1|1|00:1e:58:f4:15:f7|Nexus 4, 4.4, MAKOZ30d $1|1391436893 ?[176.08179, -13.839829, -1.0054213] %PKKV7|00:7f:28:3f:17:9d|-67|2437 %DC2VJ|f8:e4:fb:a0:06:f8|-71|2412 %VVWSP|00:7f:28:d5:92:65|-71|2462 %SVT8H|f8:e4:fb:8e:d6:9b|-77|2437 %ThreeBestFriends|20:10:7a:14:6a:f7|-66|2452 %2X4C8|00:7f:28:44:23:da|-75|2437 %STDGD|f8:e4:fb:70:86:f4|-82|2462 %DeathStar|00:7f:28:be:c8:94|-84|2412 %Freeinternet|00:1e:58:f4:15:f7|-59|2437 %QB657|00:26:62:b7:16:4b|-88|2462 %375F2|00:26:b8:3e:0a:14|-70|2412 %E1K38|00:26:62:cf:90:37|-81|2412";
        String regularExpression = "\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}\\W{1}-\\d{2}";

        Pattern pattern = Pattern.compile(regularExpression);
        Matcher matcher = pattern.matcher(testData);
        while(matcher.find()) {
            System.out.println(matcher.group(0));
        }

    }
}

生成以下输出:

00:1a:1e:87:04:42|-87
00:1a:1e:8e:e9:a2|-77
00:1a:1e:87:04:51|-95
00:1a:1e:84:92:02|-84
00:1a:1e:8d:f7:a2|-67
00:1a:1e:82:b8:e1|-56
00:1a:1e:82:b8:e2|-54
00:1a:1e:82:b8:e0|-56
00:1a:1e:87:04:41|-88
00:1a:1e:8d:f7:b1|-78
00:1a:1e:8d:f7:b2|-78

我正在尝试将输出文件保存为由列分隔的excel文件。 有没有人对如何实现这一点有任何建议?

谢谢!

1 个答案:

答案 0 :(得分:1)

只需添加一些逗号(目前您没有任何逗号),并将其另存为名为some_file_name.csv的文本文件。您可以使用例如一个BufferedWriter

说'用逗号分隔的Excel文件'不正确,实际上正确的术语是'逗号分隔值文件'或'CSV文件'。 Excel以外的程序也可以打开这些文件。