1

私が持っている質問に対する多くの答えを見てきました。私はそれらすべてを試しましたが、どれもうまくいきません。Excel ファイルをエクスポートするときに、キャリッジ リターンがあると、代わりに次の列に進むべきデータが新しい行に入力されます。

次のように、列レベルでキャリッジ リターンを削除しようとしています。

String col = columnName.replaceAll("\r", "");
             reportColumn.put( "column", col ); 

これは各ブロックを反復処理し、Excel シートに入力します。また、csv ファイル全体を含む文字列を使用して、ここでキャリッジ リターンを削除しようとしています。

String csv = "";

CSVReportGenerator generator = new CSVReportGenerator( );
generator.setReportColumns( this.reportColumns );
generator.setReportRows( rows );
generator.setApplicationPath("");
generator.setNL('\n');
generator.setDebuggingON(Config.DEBUGGING_ON);
generator.produceReport( );
csv = generator.getCSV( );

csv.replaceAll(",,", "");
csv.replaceAll(".", "");
csv.replaceAll("\r", "");
csv.replaceAll("\n", "");
csv.replaceAll("\r\n", "");
csv.replaceAll("\\r\\n", "");

ご覧のとおり、改行を削除するいくつかの異なる方法を試してみましたが、失敗しました。誰が私が間違っているのか教えてもらえますか?

4

2 に答える 2

0

あなたが試すことができます

System.getProperty("line.separator")

これの利点の1つは、プラットフォームに依存しないことです。

于 2012-04-26T15:33:15.857 に答える
0

質問を言い換えると、新しい行を含むセルを含むExcelドキュメントがあります。このドキュメントをCSVにエクスポートすると、セル内の新しい行によってCSVファイルが破損します。

改行の削除

CSVに書き込まれているときに、各セルから新しい行を削除しようとしたようですが、機能していないようです。提供するコードでは、\ r文字だけを置き換えますが、\n文字は置き換えません。私はこれを試してみます:

String col = columnName.replaceAll("[\r\n]", "");
reportColumn.put( "column", col );

これは、改行として解釈される可能性のある両方のタイプの文字をすべて置き換えます(実際、Windowsでは、改行は通常2文字を合わせたものです\ r \ n)。

改行を削除するための正規表現に関する限り、ここに要約があります。

",,"  // Removes two commas, not newlines
"."  // Removes all characters, except newlines
"\r" // Removes the "carriage return" characters (half of the Windows newline)
"\n" // Removes the "new line" characters (half of the Windows newline)
"\r\n" // Removes a Windows newline but not individual newline characters
"\\r\\n"  // Same as "\r\n" but the escapes are handled by the regex library rather than the java compiler.
"[\r\n]" // Should remove any newline character.
"[\\r\\n]" // Same as above but with extra escaping.

エスケープされた改行を書く

セル内に改行を含むCSVファイルを生成できるはずです。実際、Excel自体がこれを行うことができます。ここには、必要なすべてのエスケープを実行するExcelCSVPrinter Javaライブラリがあります:http://ostermiller.org/utils/CSV.html

これは、Excel形式のcsvの1行です。

"cell one","first line of cell two
second line of cell two","cell three"

http://ostermiller.org/utils/CSV.htmlには、そのようなExcelCSV形式を読み取るためのExcelCSVParserJavaライブラリも用意されています。

于 2012-11-30T20:16:09.743 に答える