c# ReportViewer Export - c#

In ReportView I want to export to the following formats: .docx, .pdf, or .xlsx
Export to .pdf:
reportViewer.ExportDialog(_reportViewer.LocalReport.ListRenderingExtensions()[3]);
Export to .docx:
reportViewer.ExportDialog(_reportViewer.LocalReport.ListRenderingExtensions()[5]);
Export to .xlsx:
reportViewer.ExportDialog(_reportViewer.LocalReport.ListRenderingExtensions()[1]);
But at first I have to choose a format for saving.
I want at first to open SaveFileDialog and in it to choose a format for saving
How can I do it ?

Here's the code I came up with:
string _sSuggestedName = String.Empty;
byte[] byteViewerPDF = _reportViewer.LocalReport.Render("PDF");
byte[] byteViewerExcel = _reportViewer.LocalReport.Render("Excel");
byte[] byteViewerWord = _reportViewer.LocalReport.Render("Word");
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "PDF files (*.pdf)|*.pdf| Doc files
(*.doc)|*.doc| Excel files (*.xls)|*.xls";
if (saveFileDialog1.ShowDialog() == DialogResult.Ok)
{
FileStream newFile = new FileStream(saveFileDialog1.FileName, FileMode.Create);
if (saveFileDialog1.FilterIndex == 1)
{
newFile.Write(byteViewerPDF, 0, byteViewerPDF.Length);
newFile.Close();
}
else
if (saveFileDialog1.FilterIndex == 2)
{
newFile.Write(byteViewerWord, 0, byteViewerWord.Length);
newFile.Close();
}
else
if (saveFileDialog1.FilterIndex == 3)
{
newFile.Write(byteViewerExcel, 0, byteViewerExcel.Length);
newFile.Close();
}
}

Related

C# Save Image files using SaveFileDialog or FolderBrowserDialog

I'm trying to save image files which are converted from PDF to PNG. I want my application to save the converted image if the PDF was a single page document using the "SaveFileDialog", and if the PDF file was a multi-page document, I then want my application to save them into a folder using the "FolderBrowserDialog".
My problem is that if the PDF file was a multi-page document, my code would first save the first image (after conversion) using the "SaveFileDialog" before attempting to save the rest of the images using "FolderBrowserDialog".
Here is what I've tried.
Image = imageToConvert = null;
for (int i = 0; i < images.Length; i++)
{
if (i == 0)
{
//Save converted image if PDF is single page
imageToConvert = images[i];
SaveFileDialog _saveFile = new SaveFileDialog();
_saveFile.Title = "Save file";
_saveFile.Filter = "PNG|*.png";
_saveFile.FileName = Lbl_OriginalFileName.Text;
if (_saveFile.ShowDialog() == DialogResult.OK)
{
imageToConvert.Save(_saveFile.FileName, ImageFormat.Png);
imageToConvert.Dispose();
}
else if (_saveFile.ShowDialog() == DialogResult.Cancel)
{
return;
}
}
else
{
if (i > 0)
{
// Save converted Images if PDF is multi-page
Image imageToConvert2 = images[i];
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowDialog();
fbd.Description = "Select the folder you want save your files into.";
string pathString = Path.Combine(fbd.SelectedPath, subFolder);
Directory.CreateDirectory(pathString);
if (fbd.ShowDialog() == DialogResult.Cancel)
{
return;
}
string saveFileNamesPNG = string.Format(Lbl_OriginalFileName.Text + "_" + i.ToString() + ".png", ImageFormat.Png);
imageToConvert.Save(Path.Combine(pathString, saveFileNamesPNG));
imageToConvert.Dispose();
}
}
}
I would really appreciate any help.
I moved the test outside the loop and then checked if it is one page and use the SaveFileDialog. And if there are more than one, I then used a FolderBrowserDialog with the For-loop to save the images.

Getting chosen file type from SaveFileDialog

In the SaveFileDialog with WinForms, I provide the option to save an Excel file or csv file. How can I get the option selected?
SaveFileDialog exportDialog = new SaveFileDialog();
exportDialog.Filter = "Excel spreadsheet (*.xlsx)|*.xlsx|Comma-separated values file (*.csv)|*.csv";
if (exportDialog.Filter.ShowDialog() == DialogResult.OK)
{
// do something based on chosen file type
}
You can achieve it by using FilterIndex of SaveFileDialog like this:
SaveFileDialog exportDialog = new SaveFileDialog();
exportDialog.Filter = "Excel spreadsheet (*.xlsx)|*.xlsx|Comma-separated values file (*.csv)|*.csv";
if (exportDialog.ShowDialog() == DialogResult.OK)
{
if (exportDialog.FilterIndex == 1)
{
MessageBox.Show("Excel");
}
if (exportDialog.FilterIndex == 2)
{
MessageBox.Show("CSV");
}
}
Note: Index of items will start from 1.

Replace SaveFileDialog to saving in code

I have such code that saves bin file, but user has to choose file
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Binary File (*.bin)|*.bin";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
Question.PersistObject(questions, myStream);
myStream.Close();
}
}
But I want to choose file in code, and if a file with such name doesn't exist, then create it. How to set in myStream that file?
Replace all your logic related to the OpenFileDialog with File.Open:
using (var myStream = File.Open(someFilePath, FileMode.OpenOrCreate))
{
Question.PersistObject(questions, myStream); // do something with the stream
}
The OpenOrCreate file mode will open the file if it exists, or create it if it does not.
The using statement will take care of closing the stream for you.
One option would be to have a base name and append a number:
string templateName = "myfile{0}.bin";
string finalName;
int count = 0;
do {
finalName = String.Format(templateName, count++);
} while (File.Exists(finalName);
Or, if you don't care about the name, use Path.GetTempFileName
Then pass that name to StreamWriter:
using (StreamWriter writer = new StreamWriter(finalName))
{
// Write stuff
}

save a chart together a txt file with savedialog

I have a listbox, I save it to a txt file with following code.
String[] array = new String[listBox2.Items.Count];
listBox2.Items.CopyTo(array, 0);
Microsoft.Win32.SaveFileDialog saveFileDialog1 = new Microsoft.Win32.SaveFileDialog();
saveFileDialog1.FileName = "per_" ;
saveFileDialog1.DefaultExt = ".txt";
saveFileDialog1.Filter = "Text files (.txt)|*.txt";
Nullable<bool> res = saveFileDialog1.ShowDialog();
if (res == true)
{
string filename = saveFileDialog1.FileName;
File.WriteAllLines(filename, array, Encoding.UTF8);
MessageBox.Show("File saved successfully");
}
I save chart to c://
chart2.SaveImage("C://", System.Drawing.Imaging.ImageFormat.Jpeg);
However I want to save my chart at same direction which user chosed with savefiledialog. What should I do to manage this?
Try this
chart2.SaveImage(Path.GetDirectoryName(saveFileDialog1.FileName), System.Drawing.Imaging.ImageFormat.Jpeg);
or this
chart2.SaveImage(Path.GetDirectoryName(saveFileDialog1.FileName) + "\\chart.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
if SaveImage method needs a filename

c# savefilediaglog saving text of specific length

my code is-
Stream myStream;
saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Ticket files (*.tkt)|*.tkt";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Title = "Save text Files";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
myStream = saveFileDialog1.OpenFile();
if (myStream != null)
{
StreamWriter wText = new StreamWriter(myStream);
string st = gettxt();
wText.Write(st);
//wText.WriteLine("sdfsderfsdsf");
myStream.Close();
}
}
whenever i uncomment Writeline and comment write(st) notthing is written..
and the string should be of specific length then only data is saved in the tkt file..
and last problem is that if text is too large then part of the text is only written..
please help me
try with
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
TextWriter tw = new StreamWriter(saveFileDialog1.FileName);
tw.WriteLine(gettxt());
tw.Close();
}
Hope this helps.
There are very few ways to explain the problem, other than there's a problem with the app that reads the file. Beware that you are writing a .txt file, it might not necessarily be compatible with a .tkt file format. Whatever that looks like. For one, the file will contain a BOM, 3 bytes at the start of the file that says it is a utf-8 formatted text file.
One thing you could try to diagnose the problem better:
using (var wText = new StreamWriter(myStream, Encoding.Default)) {
string st = gettxt();
wText.Write(st);
//wText.WriteLine("sdfsderfsdsf");
}
The Encoding argument specifies that the characters should be written in the default code page and without a BOM.
saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Ticket files (*.tkt)|*.tkt";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Title = "Save text Files";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream myStream = saveFileDialog1.OpenFile())
{
using (StreamWriter wText = new StreamWriter(myStream))
{
wText.WriteLine(gettext());
}
}
}

Categories