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;
}
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 am attempting to auto-fill/sign/print the federal i-9 form using Spire.PDF and C#. The i9 file is an XFA form and is protected and doesn't allow for signing. However, if I fill the i9 and print to PDF, then I can sign that new file.
The step I'm getting stuck on is printing the filled i9 to a PDF file without actually opening Acrobat or having direct interaction from the end-user to specify a file name. I say 'printing' because if I just save it as a PDF file it never flattens the XFA form and remains locked against signing.
So far I have automated printing of the file using this code:
Process proc = new Process();
proc.StartInfo.Verb = "PrintTo";
proc.StartInfo.FileName = filename;
proc.StartInfo.Arguments = "\"" + printername + "\"";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
and I think I might be able to force use of the Microsoft Print to PDF 'printer' here, but I don't know if there's a way to specify the file name to use so that the user isn't prompted?
If I try printing using the Spire.PDF control, I am only able to get a file with the "Please wait...
If this message is not eventually replaced by the proper contents of the document, your PDF
viewer may not be able to display this type of document.." message as a result.
When the form is opened to print via Acrobat I get a popup of "This form contains incomplete or invalid information. Are you sure you want to print?" If I click Yes then I can successfully print to PDF and then I can sign that file.
So, I believe whatever data-checking is happening is causing the failure to print via code and I'm hoping those wiser than I might have some ideas of ways around this issue.
Thank you in advance for your help! If you just search for Federal i9 you should find the file I'm working with. I didn't see a space to attach a file here.
This is the code that I'm using to try to accomplish my task via the Spire.PDF control.
PdfDocument doc = new PdfDocument();
string i9path = "locationofi9file"
string newi9path = "locationoffilledi9file"
doc.LoadFromFile(i9path);
/*fill form here*/
doc.Form.IsFlatten = true;
doc.SaveToFile(newi9path, FileFormat.PDF);
doc.Close();
doc.Dispose();
doc.LoadFromFile(newi9path);
string file = "printi9";
if (System.IO.File.Exists(file))
System.IO.File.Delete(file);
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
PrinterSettings settings = new PrinterSettings();
PageSettings pages = new PageSettings();
string printername = "Microsoft Print to PDF";
settings.PrinterName = printername;
settings.PrintToFile = true;
settings.PrintFileName = Path.Combine(directory, file + ".pdf");
PrintDocument printDoc = doc.PrintDocument;
printDoc.PrinterSettings = settings;
printDoc.Print();
doc.Close();
doc.Dispose();
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";
I need to create and save a Excel file without inform in the code the path and file name. So I can use the savefiledialog to show the save box to input the path and file name, but I can´t use it correctly.
I tried to use the worksheet.saveas but this class doesn´t show the save box to input the path and file name.
How can I save a excel file with that save box?
The basic mechanic of it is this:
public void SaveExcelWorkBook()
{
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.InitialDirectory = #"C:\";
openDlg.ShowDialog();
string path = openDlg.FileName;
if (openDlg.ShowDialog() == DialogResult.OK)
{
try
{
Application excelApp = new Application();
Workbook workBook = excelApp.Workbooks.Open(path);
Worksheet workSheet = (Worksheet)workBook.Worksheets[1];
// Do your work here inbetween the declaration of your workbook/worksheet
// and the save action below.
workBook.SaveAs(/*path to save it to*/); // NOTE: You can use 'Save()' or 'SaveAs()'
workBook.Close();
}
catch (Exception ex)
{
}
}
}
I think I should also mention that Interop objects are unmanaged so, you will want to make sure that you are releasing them after calling .Close(). Here is an example:
Marshal.ReleaseComObject(workBook);
There are two fantastic tutorials for using Excel here and here. Good luck!
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.