Click here to Skip to main content
15,908,455 members

Comments by EricERankin (Top 7 by date)

EricERankin 15-Apr-20 1:10am View    
Looking at your buttonSplit_Click it seems like you want to save each individual page into a separate file, is that correct?
In that case, take a look at this example of how to split PDF file in C#, it generates a PDF file for each PDF page.
EricERankin 15-Apr-20 0:49am View    
Here is the simplest was I know how to export DataGridView to an Excel file in VB.NET:

-------
Dim saveFileDialog = New SaveFileDialog()
If (saveFileDialog.ShowDialog() = DialogResult.OK) Then

Dim workbook = New ExcelFile()
Dim worksheet = workbook.Worksheets.Add("Sheet1")

' Export data from DataGridView to ExcelFile.
DataGridViewConverter.ImportFromDataGridView(worksheet, Me.dataGridView1, New ImportFromDataGridViewOptions() With {.ColumnHeaders = True})

workbook.Save(saveFileDialog.FileName)

End If
-------

This doesn't use Office Interop, instead it uses an Excel library for VB.NET.
EricERankin 15-Apr-20 0:41am View    
Excel Interop is not known for its efficiency, it is a wrapper for GUI application so the performances can be questionable at times, especially in case of large files.
If you can change your approach, then here is an alternative that uses GemBox.Spreadsheet to retrieve faster all the cells formatted values:

----
var workbook = ExcelFile.Load("input.xlsx");
var worksheet = workbook.Worksheets.ActiveWorksheet;

int rowCount = worksheet.Rows.Count;
int columnCount = worksheet.CalculateMaxUsedColumns();

string[,] values = new string[rowCount, columnCount];
for (int r = 0; r < rowCount; r++)
for (int j = 0; j < columnCount; j++)
values[r, j] = worksheet.Cells[r, j].GetFormattedValue();
----

Also, here is another example for reading Excel files from C# in a clean and efficient way.
EricERankin 26-Mar-20 13:48pm View    
Your question is a bit ambiguous, nevertheless, here is how you can export datatable into excel file:
--------
string directory = @"C:\NOTES";

if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);

var dataTable = ExcelTable();

var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Sheet1");
worksheet.InsertDataTable(dataTable, new InsertDataTableOptions() { ColumnHeaders = true });

var table = worksheet.Tables.Add("InformationTable", worksheet.GetUsedCellRange(true), true);
table.BuiltInStyle = BuiltInTableStyleName.TableStyleMedium2;

workbook.Save(Path.Combine(directory, "InformationTable.xlsx"));
--------

If you need to print an excel file from c# then just add this:

--------
workbook.Print("Your printer's name or use 'null' for default printer.");
--------
EricERankin 26-Mar-20 13:32pm View    
Let's say you have those values in an Excel file under columns A and B, here is how you would update your XML file with that data:
-----------
var workbook = ExcelFile.Load("input.xlsx");
var worksheet = workbook.Worksheets[0];

var xml = new XmlDocument();
xml.Load("box.xml");
var nodes = xml.DocumentElement.ChildNodes.Cast<XmlNode>().ToArray();

for (int index = 0; index < 4; index++)
{
var row = worksheet.Rows[index];
var node = nodes[index];

node.Attributes["X"].Value = row.Cells["A"].Value.ToString();
node.Attributes["Y"].Value = row.Cells["B"].Value.ToString();
}

xml.Save("box-updated.xml");
-----------
The code is processing Excel using this C# library.
Also, you can as a reference this full C# example for reading Excel files.
I hope this helps.