How can I avoid the multiple extensions in the retrieved file name from save dialog?
I have filtered the dialog only to Rich Text File and .doc files.
When I change the selection in the combo Box below the text Box with file name, the extension is added to the file name instead of changing the existing extension.
SaveFileDialog dialog= new SaveFileDialog();
dialog.Title = "Please select the directory in which the document will be created.";
// set a default file name
dialog.FileName = my_File_Name;
// set filters
dialog.Filter = "RTF Files (*.rtf)|*.rtf | Wordfile (*.doc)|*.doc | Text Files (*.txt)|*.txt";
if (dialog.ShowDialog() == DialogResult.OK)
{
//selected folder path
string placeToSaveDocument = Path.GetFullPath(dialog.FileName);
}
That is the result:
myFileName.rtf.doc.rtf.txt
Problem : Your Filter string is not proper as it has spaces after extension string *.rtf and *.doc
Solution : You need to eliminate the space after extension strings.Remove spaces after *.rtf and *.doc
Try This:
dialog.Filter = "RTF Files (*.rtf)|*.rtf|Wordfile (*.doc)|*.doc|Text Files (*.txt)|*.txt";
Related
Assume the following sheet in a csv file as shown in the following image:
I want to import each column data as list and skip the first row using c# in visual studio.
I need also to know how to browse only the csv file and load it using c#.
Thanks in advance
No need to reinvent the wheel on CSV file reading. Install CsvHelper via NuGet and then follow the instructions here for how to use: https://joshclose.github.io/CsvHelper/getting-started
This will save you the trouble of worrying about escaping commas, skipping the header row, handling very large files and so on.
Actually, you can think of csv files as txt files. The data in csv is saved in the following format(use comma as separator).
Name,ID,Salary
AA,102,0
AB,103,0
AC,104,0
So you can read data like reading .txt file. As to "browse only the csv file", you can set a filter for OpenFileDialog.
Here is a simple demo.
List<string> name = new List<string>();
List<string> id = new List<string>();
List<string> salary = new List<string>();
private void btOpenCSV_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open CSV";
ofd.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
ofd.InitialDirectory = "d:\\";
if (ofd.ShowDialog() == DialogResult.OK)
{
bool isfirstrow = true; // check if the first row
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(ofd.FileName);
while ((line = file.ReadLine()) != null)
{
if (isfirstrow)
{
isfirstrow = false;
continue;
}
name.Add(line.Split(',')[0]);
id.Add(line.Split(',')[1]);
salary.Add(line.Split(',')[2]);
}
}
}
I wonder how could I improve these code that it will automatically recognize file in the folder "/Doc/Projekti/<%=dat%>" and chose the right icon from "/Img/image.png". In this folder, I have icons for pdf, xls, word,...
All these icons are in png format. Here is my code I currently using but on all files that code find is now pdf icon
<%
string adresa = (string)Application["naslov"];
string adresa1 = adresa + "doc\\Projekti";
var t=2;
DirectoryInfo info = new DirectoryInfo(adresa1);
FileInfo[] files = info.GetFiles().OrderByDescending(p => p.CreationTime).ToArray();
foreach (FileInfo dat in files){
var test=dat;
if(test.ToString() != "Thumbs.db"){
%>
<img src="/Img/pdf.png" style="border: 0px" alt="PDF"><%=dat%><BR>
On the assignment of the icon, call a function that gets the file name as an input and return the matching png icon according to the file extension.
My code will generate the excel document like this
|id | Name | Address | company_Name | Destination|
|----|-------|----------|--------------|------------|
|##1 | xxx | xxxx | xxx | xxxxx |
But I want like this...
-----------------------------------------------------
| Personal Information | Working INFO |
-----------------------------------------------------
|id | Name | Address | company_Name | Destination|
|----|-------|----------|--------------|------------|
|##1 | xxx | xxxx | xxx | xxxxx |
-----------------------------------------------------
I get the data from the API and I will save it using the SaveFileDialog
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = "Save file as...";
dialog.Filter = "Text files (*.csv)|*.csv";
dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
System.IO.StreamWriter writer = new System.IO.StreamWriter(dialog.FileName); //open the file for writing.
writer.Write(report); //write the current date to the file. change this with your date or something.
writer.Close(); //remember to close the file again.
writer.Dispose(); //remember to dispose it from the memory.
program.ShowInformationMessage("File Save successfully");
}
There is no problem with it.
I want to make the heading as the inline something like it.
...
System.IO.StreamWriter writer = new System.IO.StreamWriter(dialog.FileName); //open the file for writing.
writer.Write("Personal Information" + delimiter + delimiter + "Working INFO" + delimiter);
writer.Write(report); //write the current date to the file. change this with your date or something.
...
Csv format doesn't support merged cells, but still you can arrange header line like described above. Just replace delimiter with whatever your cell delimiter is.
Have you considered using closedxml (https://closedxml.codeplex.com/).
var wb = new XLWorkbook(report); //open spreadsheet
IXLWorksheet ws = wb.Worksheets.First(); //get first sheet
ws.Row(1).InsertRowsAbove(1); //insert row
ws.Cell("A1").Value = "Personal Information";
ws.Cell("A4").Value = " Working INFO";
ws.Range("A1:A3").Row(1).Merge(); // merge first title
ws.Range("A4:A6").Row(1).Merge(); // merge second
wb.SaveAs(writer);
You can also open csv's and save as csv
Firstly create your excel file template in excel and format it as you want.
Put only one line where lots of rows will be printed.
Put labels into the data row to replace with real data.
Save it as MHT file. (styles will be saved, too like html)
Open the mht file with text editor.
Put < !#> at the begining and end of the data row so that you can split the file content from your program and populate real data lines dynamically by replacing your labels with real properties.
<!#>
<tr height=3D20 style=3D'height:15.0pt'>
<td height=3D20 class=3Dxl67 style=3D'height:15.0pt;border-top:none'>[id]=</td>
<td class=3Dxl67 style=3D'border-top:none;border-left:none'>[name]</td>
<td class=3Dxl67 style=3D'border-top:none;border-left:none'>[company<span style=3D'display:none'>]</span></td>
<td class=3Dxl67 style=3D'border-top:none;border-left:none'>[destination]=</td>
</tr>
<!#>
Save the file from text editor.
From your program, you will read the mht file content, you will split it with the < !#> and multiply the data row part, append all together and save to another file..thats it..
You need to have installed Microsoft Visual Studio Tools for Office.
After that create common .NET project and add the reference to COM object Microsoft.Office.Interop.Excel.dll via 'Add Reference..' dialog.
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(path);
//Get All available worksheets
//Excel.Sheets excelSheets = wb.Worksheets;
//Get Specific WorkSheet
string currentSheet = "Sheet1";
Excel.Worksheet newSheet = (Excel.Worksheet)wb.get_Item(currentSheet);
newSheet.Cells[i, j].HorizontalAlignment = ExcelAlignment.xlLeft; //or Excel.XlHAlign.xlHAlignLeft
I have a .pdf created through Adobe LiveCycle Designer (it's a dynamic pdf ) i want to add this pdf to my windows app
this is what i tried
File file1 = new File(fileName);
System.Xml.XmlDocument xfadoc = new System.Xml.XmlDocument();
xfadoc.LoadXml(fileName);
here's how i get filename
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = "c:\\";
dialog.Filter = "pdf files (*.pdf) | *.pdf | All Files (*.*) | *.* | xdp files (*.xdp) | *.xdp ";
dialog.FilterIndex = 2;
dialog.RestoreDirectory = true;
dialog.CheckFileExists = true;
dialog.DefaultExt = "pdf | xdp";
fileName = dialog.FileName.ToString();
but when i click on open file button and browse to where i have stored it ; it doesnot even appear
Also when i try to load this file in my C# windows app it gives me an exception at the following line
xfadoc.LoadXml(fileName);
the exception says that
'Data at root level is invalid'
If i say that i have loaded the string (filepath name) someone please tell me how can i extract the xml part only form this dynamic file
Try Filter without spaces in extensions part.
dialog.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*|xdp files (*.xdp)|*.xdp";
LoadXml loads the document from the string parameter. You want to use the Load method.
I try to tell you my problem. with ClosedXML i have SaveAs() method, but when i use SaveAs(string name), it saves my excel document to some strange folder with some strange path. so i've decide to use savefiledialog to give user posibility to select folder and name for document. how can i use savefiledialog with closedXML?
SaveAs() also have SaveAs(Path path). Can i use it?
The "strange" folder is the folder your application is running from (since you're not specifying a path).
If you want you can use the SaveFileDialog to get the path and pass it to the SaveAs method.
var saveFileDialog = new SaveFileDialog
{
Filter = "Excel files|*.xlsx",
Title = "Save an Excel File"
};
saveFileDialog.ShowDialog();
if (!String.IsNullOrWhiteSpace(saveFileDialog.FileName))
workbook.SaveAs(saveFileDialog.FileName);
var saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Excel files|*.xlsx";
//serialVal is name of a variable, not necessary at all if you don't need a specific file name
saveFileDialog.FileName = serialVal;
if (saveFileDialog.ShowDialog() == true)
{
workbook.SaveAs(saveFileDialog.FileName);
workbook.Dispose();
return;
}