Merge multiple workbooks in one (new) master workbook using C# - c#

I need some help on integrating multiple workbooks into a single master workbook. I am trying to use the following code. I would really appreciate if someone could please help me with following problems:
saving the final (master) workbook having all the worksheets
the worksheets in different workbooks should not loose formatting while copying to master workbook.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.IO;
namespace IntegrationMacro
{
class Program
{
static void Main(string[] args)
{
Excel.Application app = new Excel.Application();
app.Visible = true;
app.Workbooks.Add("");
app.Workbooks.Add(#"C:\Users\ssehgal\Documents\Excel-Files-For-Macro\WorkBook1.xls");
app.Workbooks.Add(#"C:\Users\ssehgal\Documents\Excel-Files-For-Macro\WorkBook2.xls");
for (int i = 2; i <= app.Workbooks.Count; i++)
{
for (int j = 1; j <= app.Workbooks[i].Worksheets.Count; j++)
{
Excel.Worksheet ws = (Excel.Worksheet)app.Workbooks[i].Worksheets[j];
ws.Copy(app.Workbooks[1].Worksheets[1]);
}
}
}
}
}

var app = new Application() { Visible = true };
var master = app.Workbooks.Add();
// repeat this for each workbook you need to combine...
app.Workbooks.Add(__PATH_TO_THE_WORKBOOK__)
.Worksheets.Cast<Worksheet>()
.ToList() // Move() will modify the source of enumeration, so we need to make a temp copy
.ForEach(x => x.Move(master.Sheets[1]));

Related

How to import excell file to DataGridView from a specific row in C#?

I want to import excell value from 2nd row and on to the DataGridView but dont know how. If possible I want to keep the current existing data value in datagridview is not deleted so the imported data just added after.
this is the current result of the export from the datagridview. I want it to become template to import so the imported data only reads from 2nd row and on. I dont understand at all how to import excell data to gridview so any helpful links will be very helpful. Thank you very much
You can use the Microsoft.Office.Interop.Excel library
Below is an example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace Office
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = false;
var excelBook = excelApp.Workbooks.Open(#"C:\excelFile.xls");
var excelSheet = (Excel.Worksheet)excelBook.Sheets[1];
var lastrowR = excelSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
var lastrowC = excelSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Column;
for (int i = 1; i <= lastrowC; i++)
{
dataGridView1.Columns.Add("Column"+i.ToString(), i.ToString());
}
for (int j = 1; j <= lastrowR; j++)
{
dataGridView1.Rows.Add();
}
for (int x=2; x <= 6; x++)
{
for (int y = 15; y <= 16; y++)
{
dataGridView1.Rows[y-14].Cells[x-1].Value = excelSheet.Cells[y, x].Value.ToString();
}
}
excelBook.Close();
excelApp.Quit();
}
}
}
open the Excel file and take the desired cells and drop them into the desired cells in datagridview
using OleDbConnection , OleDbDataAdapter , DataSet to do that :
Import System.Data.
Using ADO.NET to read content (SQL SELECT command like).
The content will be in DataSet
dataGridView1.DataSource = datatSet.Tables[0];
check this link to get the code Read and Import Excel File into DataSet

I try to open an Excel file with C#, I get this error: System.Runtime.InteropServices.COMException HResult=0x800A03E

I try to open an Excel file in C#, but I get this error:
System.Runtime.InteropServices.COMException HResult=0x800A03E
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using excel = Microsoft.Office.Interop.Excel;
namespace Example_Reading_A_File_From_Excel
{
class Program
{
static void Main(string[] args)
{
excel.Application x1app = new excel.Application();
excel.Workbook x1workbook = x1app.Workbooks.Open(#"‪D:\saniaertest.xlsx");
excel._Worksheet x1worksheet = x1workbook.Sheets[1];
excel.Range x1range = x1worksheet.UsedRange;
string website;
for(int i = 1; i <= 3; i++)
{
website = x1range.Cells[i][9].value2;
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(website);
Thread.Sleep(30);
driver.Close();
}
}
}
}
Normally it should open the Excel file and open the Chrome browser and navigate to all the URL that are listed in column 9. But like said before I only get the error:
System.Runtime.InteropServices.COMException HResult=0x800A03E
It says that the workbook can't be found.
That's very odd because I tried the first few lines of your code and I was getting the same error although my file did exist where I placed it.
For some reason, the Workbooks.Open is not pointing to the correct file path so
I had to update it to the following:
string file = Directory.GetCurrentDirectory() + "\\" + "Test.xlsx";
if (File.Exists(file))
{
excel.Application x1app = new excel.Application();
excel.Workbook x1workbook = x1app.Workbooks.Open(file);
excel._Worksheet x1worksheet = x1workbook.Sheets[1];
excel.Range x1range = x1worksheet.UsedRange;
}
This worked for me as I no longer get the error that the workbook cannot be found.
I suggest using the ExcelDataReader package instead. It does not require having Excel or the InteropServices installed on a machine. As a bonus it also works to read files while running in Linux.
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
// Auto-detect format, supports:
// - Binary Excel files (2.0-2003 format; *.xls)
// - OpenXml Excel files (2007 format; *.xlsx)
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
// Choose one of either 1 or 2:
// 1. Use the reader methods
do
{
while (reader.Read())
{
// reader.GetDouble(0);
}
} while (reader.NextResult());
// 2. Use the AsDataSet extension method
var result = reader.AsDataSet();
// The result of each spreadsheet is in result.Tables
}
}
-Example from:
https://github.com/ExcelDataReader/ExcelDataReader

ClosedXML - charts missing after saving

I'm using ClosedXML to change the values of some cells in a XLSX file. These cells are used to generate a chart on another sheet.
I'm not touching the chart at all with ClosedXML. In fact, I'm not even touching the sheet that contains it, I'm just changing the values of some data cells, like this:
var workbook = new XLWorkbook(#"C:\path\to\my\file.xlsx");
var dataWorksheet = workbook.Worksheet("data");
var cell = dataWorksheet.Cell(1, 2);
cell.Value = 30;
workbook.Save();
However, when I re-open the file in Excel, the chart that was on the other sheet (which I haven't touched at all) simply disappears, leaving the sheet empty.
I know ClosedXML doesn't support the CREATION of charts, but is there a way to avoid LOSING them when saving?
Unfortunately that is a limitation on ClosedXML there are a few "chart" requests on the project page:
https://github.com/ClosedXML/ClosedXML/issues?utf8=%E2%9C%93&q=is%3Aissue+chart
But fear not the XLSX format is very simple! it's just a bunch of files all zipped.
We can still use ClosedXML to edit that Worksheet save it as a temp file and then replace the sheet on the original file, here is my code:
using ClosedXML.Excel;
using Ionic.Zip;
using System.IO;
using System.Linq;
namespace ExcelTest
{
class Program
{
static void Main(string[] args)
{
string file = #"..\..\file.xlsx";
ClosedXMLTest(file);
}
static void ClosedXMLTest(string file)
{
string outFile = "fileClosedXML.xlsx";
var workbook = new XLWorkbook(file);
var dataWorksheet = workbook.Worksheet("data");
for (int i = 1; i < 10; i++)
dataWorksheet.Cell(i, i).Value = i;
workbook.SaveAs(outFile);
ReplaceSheet(outFile, file, "xl/worksheets/sheet1.xml");
}
static void ReplaceSheet(string outputFile, string inputFile, string sheetName)
{
using (var ozip = new ZipFile(outputFile))
using (var izip = new ZipFile(inputFile))
{
var osheet = ozip.Entries.Where(x => x.FileName == sheetName).FirstOrDefault();
var tempS = new MemoryStream();
osheet.Extract(tempS);
var isheet = izip.Entries.Where(x => x.FileName == sheetName).FirstOrDefault();
izip.RemoveEntry(isheet);
izip.AddEntry(isheet.FileName, tempS.ToArray());
izip.Save();
}
}
}
}
I have the full project here:
https://github.com/heldersepu/csharp-proj/tree/master/ExcelTest

Program that reads from 2 excel files and enables users to search relevant fields from both files

This is supposed to be a program that reads from 2 excel files, stores the data in two arrays and enables users to search via textbox for a specific zip/place and upon the input in the search box to be prompted with the place/s that relate/s to it via lable or vice versa.
My problem is that it takes too much time for the files to be red. I am open for suggestions how to shorten the read time.
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel ;
namespace ExcelDateilesen_1
{
public partial class Form1 : Form
{
string[] zip = new string[3000];
string[] place = new string[3000];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Excel.Application objExcel = new Excel.Application();
Excel.Workbook objWorkbook = objExcel.Workbooks.Open
(#"C:\Users\ggr\Documents\plz.xlsx");
Excel.Worksheet objWorksheet = objWorkbook.Worksheets["Tabelle1"];
Excel.Range objCell;
int i = 1;
do
{
objCell = objWorksheet.Cells[i, 1];
if (objCell.Value == null) break;
if (objCell.Value is string)
{
zip[i - 1] = objCell.Value;
}
i++;
} while (true);
objWorkbook.Close();
objExcel.Quit();
Excel.Application objExcel1 = new Excel.Application();
Excel.Workbook objWorkbook1 = objExcel1.Workbooks.Open
(#"C:\Users\ggr\Documents\ort.xlsx");
Excel.Worksheet objWorksheet1 = objWorkbook1.Worksheets["Tabelle1"];
Excel.Range objCell1;
int j = 1;
do
{
objCell1 = objWorksheet1.Cells[j, 1];
if (objCell1.Value == null) break;
if (objCell1.Value is string)
{
place[j - 1] = objCell1.Value;
}
j++;
} while (true);
objWorkbook1.Close();
objExcel1.Quit();
}
}
}
It took about 20 seconds to read each Excel file containing 3000 rows of data using your code. Instead of looping through the Excel file and looking for a null cell, I created a method (ReadSheet) that would take an Excel worksheet and return a string array of the first column of data for 3000 rows. I used an object Array to get all the cell values, and then cast it to a string array. Reading was much faster. Unfortunately I was unable to take advantage of the worksheet.UsedRange function to get all the rows no matter how many, because it kept giving me the wrong number. I will have to look into this. Hope this helps.
EDIT: Changed to UsedRange to get all cells. The problem I had was with the excel sheet I was using.
public partial class Form1 : Form
{
string[] zip;
string[] place;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Excel.Application objExcel = new Excel.Application();
Excel.Workbook objWorkbook = objExcel.Workbooks.Open(#"C:\Users\John\Desktop\cs\TestExcel2_3000.xlsx");
Excel.Worksheet objWorksheet = objWorkbook.Worksheets["Tabelle1"];
zip = ReadSheet(objWorksheet);
objWorkbook.Close();
Excel.Workbook objWorkbook1 = objExcel.Workbooks.Open(#"C:\Users\John\Desktop\cs\TestExcel3_3000.xlsx");
Excel.Worksheet objWorksheet1 = objWorkbook1.Worksheets["Tabelle1"];
place = ReadSheet(objWorksheet1);
objWorkbook1.Close();
objExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(objWorkbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objWorkbook1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel);
}
private string[] ReadSheet(Excel.Worksheet inSheet)
{
Excel.Range targetCells = inSheet.UsedRange;
//Excel.Range targetCells = inSheet.Range["A1:A3000"];
Array allValues = (Array)targetCells.Cells.Value;
return allValues.Cast<string>().ToArray();
}
}

How to copy pre-generated csv file into xlsx with c#

I'm writing a program that copy specific columns from pre-generated .csv file to template xlsx file. With my code I'am only able to achive copying the whole csv file to the first column. Any tips how to make the program see " ; " as a separator during pasting?
Also if I overpass this problem how to copy only specific columns that i choose?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
namespace Kreator
{
class Program
{
static void Main(string[] args)
{
Excel.Application srcApp;
Excel.Workbook srcWorkbook;
Excel.Worksheet srcWorksheet;
Excel.Application destApp;
Excel.Workbook destWorkbook;
Excel.Worksheet destWorksheet;
string srcPath = "C:\\Users\\Desktop\\Raport.csv";
string destPath = "C:\\Users\\Desktop\\XXX.xlsx";
srcApp = new Excel.Application();
srcWorkbook = srcApp.Workbooks.Open(srcPath);
srcWorksheet = srcWorkbook.Worksheets.get_Item(1);
destApp = new Excel.Application();
destWorkbook = destApp.Workbooks.Open(destPath,0,false);
destWorksheet = destWorkbook.Worksheets.get_Item(1);
Excel.Range srcRange = srcWorksheet.get_Range("A1", "C20");
Excel.Range destRange = destWorksheet.get_Range("C10","E29");
srcRange.Copy(Type.Missing);
destRange.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValu es, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone);
destWorkbook.SaveAs("C:\\Users\\Desktop\\GotowyPlik" + DateTime.Now.ToString("MM_dd_yyyy") + ".xlsx");
srcApp.Application.DisplayAlerts = false;
destApp.Application.DisplayAlerts = false;
srcWorkbook.Close(false, null, null);
destWorkbook.Close(true, null, null);
destApp.Quit();
srcApp.Quit();
}
}
}

Categories