How to used a blank delimiter when exporting CSV - crystal-reports

I would like to export crystal report into csv but withouter a delimter character around fields,
here is a snipper of my code:
ExportOptions exportOpts = new ExportOptions();
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
exportOpts.ExportFormatType = ExportFormatType.CharacterSeparatedValues;
DiskFileDestinationOptions DiskFileDestinationOpt = ExportOptions.CreateDiskFileDestinationOptions();
DiskFileDestinationOpt.DiskFileName = filename;
exportOpts.ExportDestinationOptions = DiskFileDestinationOpt;
CharacterSeparatedValuesFormatOptions csvOptions = new CharacterSeparatedValuesFormatOptions();
csvOptions.Delimiter = "";
csvOptions.SeparatorText = "";
exportOpts.ExportFormatOptions = csvOptions;
crystalReportDoc.Export(exportOpts);
the problem is whenever I use an empty string for the delimiter property, crystal report will use the default double quote character in the result csv file.
can someone please assist on how to export a csv document with a blank delimiter?

If you use a blank delimiter, it's not a csv file. (Comma-separated value) You're probably looking for more of a "Text" file type.

Related

Loading csv to postgresql using copy from command

I'm trying to load data into postgresql from csv. During this process, I come across certain issue:-
here is my code:-
stmt1 = conPost.createStatement();
ResultSet rs = stmt1.executeQuery(sqlQuery);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount=rsmd.getColumnCount();
for(int i=1; i<=columnCount; i++) {
if(rsmd.getColumnTypeName(i) == "varchar" || rsmd.getColumnTypeName(i) == "text" || rsmd.getColumnTypeName(i) == "char") {
header.add(rsmd.getColumnName(i));
}
}
if(header.isEmpty()) {
copyQuery="COPY "+this.tableName+"("+columns+") FROM STDIN WITH DELIMITER '"+this.delimit+"' CSV HEADER";
} else {
strListString = header.toString();
strListString = strListString.replaceAll("\\[|\\]", "");
copyQuery="COPY "+this.tableName+"("+columns+") FROM STDIN WITH DELIMITER '"+this.delimit+"' CSV HEADER FORCE NULL " + strListString;
}
Previously, I come across issue that, NULL values in csv for varchar,text columns are as "" . So, I used FORCE NULL option on varchar and text type columns so that it will insert "" NULL only. Now it is working fine.
Now the new issue is:- if in my csv text data is like
"Hi, "vignesh". How do you do"
After loading into postgres:- it is like
Hi, vignesh. How do you do
I mean, suppose if inside the text data in csv contains double quotes, it is not inserting it as double quotes in postgres.
Is I'm missing something??? How can we overcome this??? Thanks in Advance
I would state that the problem is in your source string.
"Hi, "vignesh". How do you do"
Contains " also in the middle of the string, making it parsable incorrectly.
I would try to alter the string to be something similar to
"Hi, ""vignesh"". How do you do"
I tested the case with a file composed by
1,"Hi, ""vignesh"". How do you do"
And a table made of
create table testtable (id int, text varchar);
when executing the
copy testtable from 'data.csv' delimiter ',' CSV;
I end up with
id | text
----+------------------------------
1 | Hi, "vignesh". How do you do

Change date column to integer

I have a large csv file as below:
DATE status code value value2
2014-12-13 Shipped 105732491-20091002165230 0.000803398 0.702892835
2014-12-14 Shipped 105732491-20091002165231 0.012925206 1.93748834
2014-12-15 Shipped 105732491-20091002165232 0.000191278 0.004772389
2014-12-16 Shipped 105732491-20091002165233 0.007493046 0.44883348
2014-12-17 Shipped 105732491-20091002165234 0.022015049 3.081006137
2014-12-18 Shipped 105732491-20091002165235 0.001894693 0.227268466
2014-12-19 Shipped 105732491-20091002165236 0.000312871 0.003113062
2014-12-20 Shipped 105732491-20091002165237 0.001754068 0.105016053
2014-12-21 Shipped 105732491-20091002165238 0.009773315 0.585910214
:
:
What i need to do is remove the header and change the date format to an integer yyyymmdd (eg. 20141217)
I am using opencsv to read and write the file.
Is there a way where i can change all the dates at once without parsing them one by one?
Below is my code to remove the header and create a new file:
void formatCsvFile(String fileToChange) throws Exception {
CSVReader reader = new CSVReader(new FileReader(new File(fileToChange)), CSVParser.DEFAULT_SEPARATOR, CSVParser.NULL_CHARACTER, CSVParser.NULL_CHARACTER, 1)
info "Read all rows at once"
List<String[]> allRows = reader.readAll();
CSVWriter writer = new CSVWriter(new FileWriter(fileToChange), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER)
info "Write all rows at once"
writer.writeAll(allRows)
writer.close()
}
Please can some one help?
Thanks
You don't need to parse the dates, but you do need to process each line in the file and convert the data on each line you want to convert. Java/Groovy doesn't have anything like awk where you can work with file data as columns, for example, the first 10 "columns" (characters usually) in every line in a file. Java/Groovy only deals with "rows" of data in a file, not "columns".
You could try something like this: (in Groovy)
reader.eachLine { String theLine ->
int idx = theLine.indexOf(' ')
String oldDate = theLine.subString(0, idx)
String newDate = oldDate.replaceAll('-', '')
String newLine = newDate + theLine.subString(idx);
writer.writeLine(newline);
}
Edit:
If your CSVReader class is not derived from File, then you can't use Groovy's eachLine method on it. And if the CSVReader class's readAll() method really returns a List of String arrays, then the above code could change to this:
allRows.each { String[] theLine ->
String newDate = theLine[0].replaceAll('-', '')
writer.writeLine(newDate + theLine[1..-1])
}
Ignore the first line (the header):
List<String[]> allRows = reader.readAll()[1..-1];
and replace the '-' in the dates by splitting each row and editting the first:
allrows = allrows.collect{
row -> row.split(',')[0].replace(',','') // the date
+ row.split(',')[1..-1] // the rest
}
I don't know what you mean by "all dates at once". For me can only be iterated.

How to specify strings in Weka file?

I am working on a text classification system and I would like to use unigrams as features. When building the arff file, I declared a string attribute field inside which I want to specify all the words contained in a message separated by comma. However, Weka is telling me that it "Cannot handle string attributtes". I tried defining the relation in the header with StringToWordVector, but it didn't help. How to go about this otherway? Many thanks!
if your arff file format is correct then the following code can help you
// dataSource: arff file (path of your arff file)
BufferedReader trainReader = new BufferedReader(new FileReader(dataSource));
trainInsts = new Instances(trainReader);
trainInsts.setClassIndex(trainInsts.numAttributes() - 1);
// the filter is used to convert the data from string to numeric
StringToWordVector STWfilter = new StringToWordVector();
FilteredClassifier model = new FilteredClassifier();
model.setFilter(STWfilter);
STWfilter.setInputFormat(trainInsts);
// the converted data
trainInsts = Filter.useFilter(trainInsts, STWfilter);

CSV export repeats headers with each field value

I know this is going to be a really simple answer... when I export my header comes out on each export line preceding the fields that are being exported. I want the header line to export in csv in the 1st line, with the record lines underneath.
When you select CSV as your export option it'll open a new dialog box. In it, note the Report and Page Sections section. Leave it set to Export but be sure to check Isolate Report/Page sections. It'll then work how you'd expect.
You can display the crystal reports as CSV files in the same way as they appear in reports by doing the below thing. This will export into pdf excel or word in exactly same way you want to display
string contentType = "";
ExportOptions options = new ExportOptions();
switch (formatType.ToLower())
{
case "pdf":
default:
options.ExportFormatType = ExportFormatType.PortableDocFormat;
contentType = "application/pdf";
break;
case "excel":
options.ExportFormatType = ExportFormatType.Excel;
contentType = "application/vnd.ms-excel";
break;
case "csv":
contentType = "application/csv";
options.ExportFormatType = ExportFormatType.CharacterSeparatedValues;
//CharacterSeparatedValuesFormatOptions v= ExportOptions.CreateCharacterSeparatedValuesFormatOptions();
//v.SeparatorText
options.ExportFormatOptions = new CharacterSeparatedValuesFormatOptions()
{
ExportMode = CsvExportMode.Standard,
GroupSectionsOption= CsvExportSectionsOption.ExportIsolated,
ReportSectionsOption = CsvExportSectionsOption.ExportIsolated
};
break;
}
This discussion suggests a couple options:
Export to Excel first, then save as CSV
Modify some registry keys
http://sagecity.na.sage.com/support_communities/sage100_erp/f/97/p/38336/125272

How to limit export formats in crystal reports

For Crystal reports for visual studio .net 2005, you can export the report
to various file format such as pdf, excel, word, rpt etc. If I just want to
limit the user see only excel and word format and set the default file
format to excel, is there a way to do it? Sometimes too many choose is
not good, is it?
Using CRVS2010 , you can remove unwanted export Option.
A new feature of CRVS2010 is the ability to modify the available export formats from the viewer export button. The following C# sample code demonstrates how to set the CrystalReportViewer to export only to PDF and Excel file formats:
int exportFormatFlags = (int)(CrystalDecisions.Shared.ViewerExportFormats.PdfFormat | CrystalDecisions.Shared.ViewerExportFormats.ExcelFormat);
CrystalReportViewer1.AllowedExportFormats = exportFormatFlags;
For More Details Please refer below link..
http://scn.sap.com/community/crystal-reports-for-visual-studio/blog/2011/01/26/export-file-formats-for-sap-crystal-reports-for-vs-2010
Try this:
Dim formats As Integer
formats = (CrystalDecisions.Shared.ViewerExportFormats.PdfFormat Or CrystalDecisions.Shared.ViewerExportFormats.XLSXFormat)
CrystalReportViewer1.AllowedExportFormats = formats
You don't mention whether you are using C# / VB.NET or Web/WinForms.
C#
I don't think this is possible. You would have to implement your own Export Button.
Something along the lines of this MSDN article
C#
// Declare variables and get the export options.
ExportOptions exportOpts = new ExportOptions();
ExcelFormatOptions excelFormatOpts = new ExcelFormatOptions ();
DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
exportOpts = Report.ExportOptions;
// Set the excel format options.
excelFormatOpts.ExcelUseConstantColumnWidth = true;
exportOpts.ExportFormatType = ExportFormatType.Excel;
exportOpts.FormatOptions = excelFormatOpts;
// Set the disk file options and export.
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
diskOpts.DiskFileName = fileName;
exportOpts.DestinationOptions = diskOpts;
Report.Export ();
VB.NET
' Declare variables and get the export options.
Dim exportOpts As New ExportOptions()
Dim diskOpts As New DiskFileDestinationOptions()
Dim excelFormatOpts As New ExcelFormatOptions()
exportOpts = Report.ExportOptions
' Set the excel format options.
excelFormatOpts.ExcelTabHasColumnHeadings = true
exportOpts.ExportFormatType = ExportFormatType.Excel
exportOpts.FormatOptions = excelFormatOpts
' Set the export format.
exportOpts.ExportFormatType = ExportFormatType.Excel
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
' Set the disk file options.
diskOpts.DiskFileName = fileName
exportOpts.DestinationOptions = diskOpts
Report.Export()
VB.NET
You used to be able to remove certain export DLLs from the client installation. i.e remove all apart from the Excel DLLs and then it would only display the export options as Excel
To disable Crystal Report Rpt format try this :
Dim formats As Integer
formats = (CrystalDecisions.Shared.ViewerExportFormats.AllFormats Xor CrystalDecisions.Shared.ViewerExportFormats.RptFormat)
CrystalReportViewer1.AllowedExportFormats = formats
Or Short Version :
CrystalReportViewer1.AllowedExportFormats = (CrystalDecisions.Shared.ViewerExportFormats.AllFormats Xor CrystalDecisions.Shared.ViewerExportFormats.RptFormat)