- 浏览: 468392 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (146)
- Maven (3)
- Quartz (10)
- Hessian (3)
- JDK (42)
- eclipse (4)
- 设计思想 (6)
- XML (8)
- JavaMail (1)
- Spring (11)
- mina (1)
- HsqlDb (1)
- Cache (2)
- Tool (6)
- 心情 (5)
- JQuery (0)
- Hadoop (5)
- Hbase (3)
- 自动构建 (7)
- JNDI (0)
- 代码赏析 (5)
- Oracle (1)
- Excel (4)
- Effective Java (5)
- JAXB (4)
- fdafasdf (1)
- ccc (0)
- web (3)
- concurrent (1)
- CVS (1)
- eclipse plugin (2)
- Apache (10)
最新评论
-
chxiaowu:
nice!
Quartz实现固定执行次数 -
zxjlwt:
学习了。http://surenpi.com
自定义ClassLoader -
kadlly:
public static final Logger log ...
Hessian 权限认证 -
spring_springmvc:
java程序语言学习教程 地址http://www.zuida ...
Java-Final -
liushuiwuyan:
[img][/img]
设计模式-单例
If there is a big data need to be export, don't use POI, or other tools. because it is slowly, Excel should be generator the font, background, border, merge cells, freezed cellls etc. you should be know one thing, "the function more power, the system more complex, the speed more slowly.".
so use the easy api to export csv, then the user can translate the file to excel or other format , whatever.
this method is copy from opencsv, not very complex, use reader, every time writer one line.
this is copy from opencsv, the reader speed is very well, will be reader one line at once.
so copy the test method.
after you generator the csv file, you can reader it.
perfect, you export your data, and read your data, this is called persisdent to file system.
so use the easy api to export csv, then the user can translate the file to excel or other format , whatever.
package ycl.learn.excel.csv; /** Copyright 2005 Bytecode Pty Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; import java.util.Iterator; import java.util.List; /** * A very simple CSV writer released under a commercial-friendly license. * * @author Glen Smith * */ public class CSVWriter { private Writer rawWriter; private PrintWriter pw; private char separator; private char quotechar; private String lineEnd; /** The character used for escaping quotes. */ public static final char ESCAPE_CHARACTER = '"'; /** The default separator to use if none is supplied to the constructor. */ public static final char DEFAULT_SEPARATOR = ','; /** * The default quote character to use if none is supplied to the * constructor. */ public static final char DEFAULT_QUOTE_CHARACTER = '"'; /** The quote constant to use when you wish to suppress all quoting. */ public static final char NO_QUOTE_CHARACTER = '\u0000'; /** Default line terminator uses platform encoding. */ public static final String DEFAULT_LINE_END = "\n"; /** * Constructs CSVWriter using a comma for the separator. * * @param writer * the writer to an underlying CSV source. */ public CSVWriter(Writer writer) { this(writer, DEFAULT_SEPARATOR); } /** * Constructs CSVWriter with supplied separator. * * @param writer * the writer to an underlying CSV source. * @param separator * the delimiter to use for separating entries. */ public CSVWriter(Writer writer, char separator) { this(writer, separator, DEFAULT_QUOTE_CHARACTER); } /** * Constructs CSVWriter with supplied separator and quote char. * * @param writer * the writer to an underlying CSV source. * @param separator * the delimiter to use for separating entries * @param quotechar * the character to use for quoted elements */ public CSVWriter(Writer writer, char separator, char quotechar) { this(writer, separator, quotechar, "\n"); } /** * Constructs CSVWriter with supplied separator and quote char. * * @param writer * the writer to an underlying CSV source. * @param separator * the delimiter to use for separating entries * @param quotechar * the character to use for quoted elements * @param lineEnd * the line feed terminator to use */ public CSVWriter(Writer writer, char separator, char quotechar, String lineEnd) { this.rawWriter = writer; this.pw = new PrintWriter(writer); this.separator = separator; this.quotechar = quotechar; this.lineEnd = lineEnd; } /** * Writes the entire list to a CSV file. The list is assumed to be a * String[] * * @param allLines * a List of String[], with each String[] representing a line of * the file. */ public void writeAll(List allLines) { for (Iterator iter = allLines.iterator(); iter.hasNext();) { String[] nextLine = (String[]) iter.next(); writeNext(nextLine); } } /** * Writes the next line to the file. * * @param nextLine * a string array with each comma-separated element as a separate * entry. */ public void writeNext(String[] nextLine) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < nextLine.length; i++) { if (i != 0) { sb.append(separator); } String nextElement = nextLine[i]; if (nextElement == null) continue; if (quotechar != NO_QUOTE_CHARACTER) sb.append(quotechar); for (int j = 0; j < nextElement.length(); j++) { char nextChar = nextElement.charAt(j); if (nextChar == quotechar) { sb.append(ESCAPE_CHARACTER).append(nextChar); } else if (nextChar == ESCAPE_CHARACTER) { sb.append(ESCAPE_CHARACTER).append(nextChar); } else { sb.append(nextChar); } } if (quotechar != NO_QUOTE_CHARACTER) sb.append(quotechar); } sb.append(lineEnd); pw.write(sb.toString()); } /** * Close the underlying stream writer flushing any buffered content. * * @throws IOException if bad things happen * */ public void close() throws IOException { pw.flush(); pw.close(); rawWriter.close(); } }
this method is copy from opencsv, not very complex, use reader, every time writer one line.
package ycl.learn.excel.csv; /** Copyright 2005 Bytecode Pty Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.List; /** * A very simple CSV reader released under a commercial-friendly license. * * @author Glen Smith * */ public class CSVReader { private BufferedReader br; private boolean hasNext = true; private char separator; private char quotechar; private int skipLines; private boolean linesSkiped; /** The default separator to use if none is supplied to the constructor. */ public static final char DEFAULT_SEPARATOR = ','; /** * The default quote character to use if none is supplied to the * constructor. */ public static final char DEFAULT_QUOTE_CHARACTER = '"'; /** * The default line to start reading. */ public static final int DEFAULT_SKIP_LINES = 0; /** * Constructs CSVReader using a comma for the separator. * * @param reader * the reader to an underlying CSV source. */ public CSVReader(Reader reader) { this(reader, DEFAULT_SEPARATOR); } /** * Constructs CSVReader with supplied separator. * * @param reader * the reader to an underlying CSV source. * @param separator * the delimiter to use for separating entries. */ public CSVReader(Reader reader, char separator) { this(reader, separator, DEFAULT_QUOTE_CHARACTER); } /** * Constructs CSVReader with supplied separator and quote char. * * @param reader * the reader to an underlying CSV source. * @param separator * the delimiter to use for separating entries * @param quotechar * the character to use for quoted elements */ public CSVReader(Reader reader, char separator, char quotechar) { this(reader, separator, quotechar, DEFAULT_SKIP_LINES); } /** * Constructs CSVReader with supplied separator and quote char. * * @param reader * the reader to an underlying CSV source. * @param separator * the delimiter to use for separating entries * @param quotechar * the character to use for quoted elements * @param line * the line number to skip for start reading */ public CSVReader(Reader reader, char separator, char quotechar, int line) { this.br = new BufferedReader(reader); this.separator = separator; this.quotechar = quotechar; this.skipLines = line; } /** * Reads the entire file into a List with each element being a String[] of * tokens. * * @return a List of String[], with each String[] representing a line of the * file. * * @throws IOException * if bad things happen during the read */ public List readAll() throws IOException { List allElements = new ArrayList(); while (hasNext) { String[] nextLineAsTokens = readNext(); if (nextLineAsTokens != null) allElements.add(nextLineAsTokens); } return allElements; } /** * Reads the next line from the buffer and converts to a string array. * * @return a string array with each comma-separated element as a separate * entry. * * @throws IOException * if bad things happen during the read */ public String[] readNext() throws IOException { String nextLine = getNextLine(); return hasNext ? parseLine(nextLine) : null; } /** * Reads the next line from the file. * * @return the next line from the file without trailing newline * @throws IOException * if bad things happen during the read */ private String getNextLine() throws IOException { if (!this.linesSkiped) { for (int i = 0; i < skipLines; i++) { br.readLine(); } this.linesSkiped = true; } String nextLine = br.readLine(); if (nextLine == null) { hasNext = false; } return hasNext ? nextLine : null; } /** * Parses an incoming String and returns an array of elements. * * @param nextLine * the string to parse * @return the comma-tokenized list of elements, or null if nextLine is null * @throws IOException if bad things happen during the read */ private String[] parseLine(String nextLine) throws IOException { if (nextLine == null) { return null; } List tokensOnThisLine = new ArrayList(); StringBuffer sb = new StringBuffer(); boolean inQuotes = false; do { if (inQuotes) { // continuing a quoted section, reappend newline sb.append("\n"); nextLine = getNextLine(); if (nextLine == null) break; } for (int i = 0; i < nextLine.length(); i++) { char c = nextLine.charAt(i); if (c == quotechar) { // this gets complex... the quote may end a quoted block, or escape another quote. // do a 1-char lookahead: if( inQuotes // we are in quotes, therefore there can be escaped quotes in here. && nextLine.length() > (i+1) // there is indeed another character to check. && nextLine.charAt(i+1) == quotechar ){ // ..and that char. is a quote also. // we have two quote chars in a row == one quote char, so consume them both and // put one on the token. we do *not* exit the quoted text. sb.append(nextLine.charAt(i+1)); i++; }else{ inQuotes = !inQuotes; // the tricky case of an embedded quote in the middle: a,bc"d"ef,g if(i>2 //not on the begining of the line && nextLine.charAt(i-1) != this.separator //not at the begining of an escape sequence && nextLine.length()>(i+1) && nextLine.charAt(i+1) != this.separator //not at the end of an escape sequence ){ sb.append(c); } } } else if (c == separator && !inQuotes) { tokensOnThisLine.add(sb.toString()); sb = new StringBuffer(); // start work on next token } else { sb.append(c); } } } while (inQuotes); tokensOnThisLine.add(sb.toString()); return (String[]) tokensOnThisLine.toArray(new String[0]); } /** * Closes the underlying reader. * * @throws IOException if the close fails */ public void close() throws IOException{ br.close(); } }
this is copy from opencsv, the reader speed is very well, will be reader one line at once.
so copy the test method.
package ycl.learn.excel.csv; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import ycl.learn.excel.ExcelHeader; import ycl.learn.excel.ExcelHeader.ColumnHeader; public class TestCSVWriter { private static final String DOWN_FILE = "C:\\Documents and Settings\\e557400\\workspace\\YclPlmTest\\DownUserHeader.csv"; /** * @param args */ public static void main(String[] args)throws Exception { // TODO Auto-generated method stub ColumnHeader[] chs = ExcelHeader.UserHeader.values(); List<String[]> dataMapList = new ArrayList<String[]>(); int j = 0; String[] dataMap = new String[chs.length]; for(ColumnHeader ch:chs){ dataMap[j] = ch.getName(); j++; } dataMapList.add(dataMap); for(int i=0;i<10;i++){ dataMap = new String[chs.length]; j=0; for(ColumnHeader ch:chs){ if(ch == ExcelHeader.UserHeader.ACTION){ dataMap[j] = "i \"love\" you"; } else if(ch == ExcelHeader.UserHeader.FIRST_NAME){ dataMap[j] = "2012-01-01"; } else if(ch == ExcelHeader.UserHeader.LAST_NAME){ dataMap[j] = "1.2365145"; } else if(ch == ExcelHeader.UserHeader.E_MAIL){ dataMap[j] = "aa"; } else{ dataMap[j] = "aanewnew2007"; } j++; } dataMapList.add(dataMap); } Writer writer = new FileWriter(DOWN_FILE); CSVWriter csvWriter = new CSVWriter(writer); csvWriter.writeAll(dataMapList); csvWriter.close(); System.out.println("writer complete"); } }
after you generator the csv file, you can reader it.
package ycl.learn.excel.csv; import java.io.FileReader; import java.io.Reader; import java.util.List; public class TestCSVReader { private static final String DOWN_FILE = "C:\\Documents and Settings\\e557400\\workspace\\YclPlmTest\\DownUserHeader.csv"; public static void main(String[] args)throws Exception { Reader reader = new FileReader(DOWN_FILE); CSVReader csvreader = new CSVReader(reader); List<String[]> list = csvreader.readAll(); for(String[] line :list){ for(String token:line){ System.out.print(token+","); } System.out.println(); } } }
perfect, you export your data, and read your data, this is called persisdent to file system.
相关推荐
本压缩包“Csv导入导出.rar”提供了一套关于CSV文件导入与导出的解决方案,特别地,它可能包含了类别选择功能,使得数据处理更具灵活性。 1. CSV文件结构: CSV文件的简单结构使其易于读写,通常由列标题和数据行...
javaCSV导入导出 ... File file = new File("C:/name.csv"); FileOutputStream out = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(out); BufferedWriter bw = new ...
本项目"PHP Excel导入导出 CSV导入导出.zip"是针对PHP环境中,如何进行Excel与CSV文件的导入导出操作的一个实例。下面我们将详细探讨相关的知识点。 1. **PHPExcel库**:在PHP中,处理Excel文件通常会用到PHPExcel...
csv 总结导入导出 已经自测过,没有问题
总之,这个项目涵盖了C#中处理CSV文件、数据可视化以及数据库操作的基本技巧,对于学习和开发涉及数据导入导出和数据库管理的应用非常有帮助。通过深入理解并实践这些知识点,你将能够更有效地管理和操作数据。
本章主要说明如何使用Magicodes.IE.Csv进行Csv导入导出. 关于Magicodes.IE 导入导出通用库,通过导入导出DTO模型来控制导入和导出,支持Excel、Word、Pdf和Html。 GitHub地址:...
CSV文件导入MySQL数据库工具软件 CSVtoDB v1.0.zip 因手头项目需要向MYSQL数据库导入大量的EXCEL数据。网上搜索了相关博客,受限于功能或者本人有一点点的小强迫,业余时间开发了本工具。 EXCEL导入到MYSQL有多种...
- PluginConfig.json:这是插件配置文件,用于定义插件的行为、设置和依赖项,可能包含了关于CSV导入导出的相关配置信息。 - LICENSE.txt:通常包含软件的许可协议,规定了使用这些组件的条件和限制。 - ...
"SQLite在ARM平台上的移植和CSV导入导出" SQLite是一种轻量级的关系型数据库管理系统,它广泛应用于嵌入式系统和移动设备中。ARM平台是目前最流行的嵌入式系统架构之一,因此将SQLite移植到ARM平台上非常有必要。...
总结,PHP中的CSV导入导出涉及的主要知识点包括:`fgetcsv()`函数用于读取CSV文件,`fputcsv()`用于写入CSV文件,以及处理特殊字符和错误处理。通过理解这些概念,你可以轻松地在PHP项目中实现CSV数据的导入导出功能...
2. 兼容性:几乎所有的电子表格软件(如Microsoft Excel、Google Sheets、Apple Numbers等)都支持CSV文件的导入和导出,使得数据交换变得简单。 3. 通用性:CSV是跨平台的,可以在Windows、MacOS、Linux等操作系统...
csv的文件的导入导出可能会用的到,需要的可以下一下把
总结来说,CSV导入导出到SQL数据库是ASP.NET开发中的常见任务,通过理解CSV文件格式和掌握数据库操作技巧,可以轻松实现数据的双向交换。在实际项目中,务必遵循良好的编程实践,保证代码的可读性和可维护性。
csv 格式文件导出数据,数据导入csv文件
标题"thinkphp下导入导出csv文件"涉及到的主要知识点包括: 1. CSV文件格式:CSV文件是以逗号分隔的纯文本文件,每个记录占据一行,每个字段之间由逗号分隔。这种格式使得数据可以轻松地在各种应用程序之间传递,如...
在IT行业中,数据的导入与导出是常见的操作,特别是在数据分析、备份或系统迁移时。本项目关注的是如何使用Java编程语言将数据库中的记录导出为CSV(Comma Separated Values)格式的文件。CSV因其通用性和易于处理的...
在Windows Presentation Foundation (WPF)应用开发中,DataGrid组件是一种强大的数据展示工具,它可以用于显示...通过学习和理解这段代码,开发者可以轻松地在自己的项目中实现DataGrid与Excel之间的数据导入导出功能。
Java 实现 CSV 文件导入与导出功能 Java 中实现 CSV 文件导入与导出的知识点主要包括以下几个方面: 一、CSV 文件简介 ...CSV 文件导入导出功能可以用于各种业务场景,如数据导入、数据导出、数据报表等。