The code here grabs a name in my listbox(employeebox) and deletes the entire row on that sheet. This works and there are no errors.. My problem is that I have 19 different sheets that need to be checked, but I can only check 1 at a time with this code.. "Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[2];".. Is there any way that I can check all the sheets once the Delete button is clicked? I greatly appreciate anyone's help.
private void delete_Click(object sender, EventArgs e)
{
//create excel
Excel.Application xlexcel = new Excel.Application();
Excel.Workbook xlWorkBook = xlexcel.Workbooks.Open(#"C:\\SAMPLE.xlsx");
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[2];
//search within excel
Excel.Range usedRanage = xlWorkSheet.UsedRange;
foreach (Excel.Range row in usedRanage)
{
//grab name once selected in box
if (employeeBox.SelectedItem.Equals(row.Value))
{
row.EntireRow.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
MessageBox.Show("Employee Deleted.");
}
}
xlexcel.DisplayAlerts = false;
xlWorkBook.SaveAs("C:\\SAMPLE.xlsx", Excel.XlFileFormat.xlWorkbookDefault, Type.Missing,
Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlWorkBook.Close();
xlexcel.Quit();
releaseObject(xlexcel);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
}
private void delete_Click(object sender, EventArgs e)
{
//create excel
Excel.Application xlexcel = new Excel.Application();
Excel.Workbook xlWorkBook = xlexcel.Workbooks.Open(#"C:\\SAMPLE.xlsx");
int[] Cols = { 1 };
Excel.Range curRange;
foreach (Excel.Worksheet sheet in xlWorkBook.Worksheets)
{
foreach (Excel.Range row in sheet.UsedRange.Rows)
{
foreach (int c in Cols)
{
curRange = (Excel.Range)row.Cells[1, 1];
if (curRange.Cells.Value != null)
{
if (employeeBox.SelectedItem.Equals(sheet.Cells[row.Row, c].Value.ToString()))
{
row.EntireRow.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
MessageBox.Show("Employee Deleted.");
}
}
}
}
}
xlexcel.DisplayAlerts = false;
xlWorkBook.SaveAs("C:\\SAMPLE.xlsx", Excel.XlFileFormat.xlWorkbookDefault, Type.Missing,
Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlWorkBook.Close();
xlexcel.Quit();
releaseObject(xlexcel);
releaseObject(xlWorkBook);
}
Related
I am trying to add pdf file in Excel using shapes object. But every time I am getting following exception:
Cannot insert object.
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.Shapes.AddOLEObject(Object ClassType, Object Filename, Object Link, Object DisplayAsIcon, Object IconFileName, Object IconIndex, Object IconLabel, Object Left, Object Top, Object Width, Object Height)
at WordSample.Form1.button1_Click(Object sender, EventArgs e) in C:\Test Projects\WordSample\WordSample\Form1.cs:line 30
Code:
private void button1_Click(object sender, EventArgs e)
{
try
{
Excel.Application app = new Excel.Application();
Excel.Workbook wb = null;
wb = app.Workbooks.Open(#"C:\Temp_Statoil\mal_malingsprogram.xls",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
InsertStaticIndexSheetsPreWpkTemplate(wb);
Excel._Worksheet wsMal = (Excel._Worksheet)wb.Sheets[pageTemplate];
Excel._Worksheet wsTotalOverView = (Excel._Worksheet)wb.Sheets[pageTotal];
//foreach (Excel.Worksheet ws in wb.Worksheets)
// sheetNames.Add(ws.Name);
wsMal.Copy(wsMal, Type.Missing);
Excel._Worksheet wsCurrent = (Excel._Worksheet)wb.Sheets[(wsMal.Index - 1)];
Excel.Range unitIcon = wsCurrent.get_Range("K11", Type.Missing);
Excel.Shape shape = wsCurrent.Shapes.AddOLEObject(
Type.Missing,
#"C:\Temp_Statoil\tmpFiles\1C9.pdf",
false,
true,
#"C:\Temp_Statoil\pdf.ico",
0,
"Doubleclick to open drawing for 1C9",
unitIcon.Left,
unitIcon.Top,
Type.Missing,
Type.Missing);
shape.Locked = false;
shape.Name = "Unit drawing [1C9]";
Marshal.ReleaseComObject(shape);
((Excel._Worksheet)wb.Sheets[1]).Activate();
wb.SaveAs(
Filename: #"C:\temp\test"+DateTime.Now,
FileFormat: Excel.XlFileFormat.xlOpenXMLWorkbook,
Password: Type.Missing,
WriteResPassword: Type.Missing,
ReadOnlyRecommended: Type.Missing,
CreateBackup: Type.Missing,
AccessMode: Excel.XlSaveAsAccessMode.xlNoChange,
ConflictResolution: Type.Missing,
TextCodepage: Type.Missing,
Local: Type.Missing);
wb.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
protected void InsertStaticIndexSheetsPreWpkTemplate(Excel.Workbook wb)
{
//Insert static sheets in "Innhold-arket" (english: Index sheet).
foreach (Excel.Worksheet ws in wb.Sheets)
if (ws.Name.ToLower() != pageTemplate.ToLower())
InsertIndex(wb, ws.Name, "", "", "", ws.Name);
else
break;
}
public void InsertIndex(Excel.Workbook wb, string sheetName, string lv1, string lv2, string lv3, string linkDescription)
{
Excel.Worksheet indexSheet = (Excel.Worksheet)wb.Sheets[pageContents];
indexSheet.get_Range(string.Format("A{0}", nextIndexNo + firstIndexLine), Type.Missing).Value = nextIndexNo;
indexSheet.get_Range(string.Format("B{0}", nextIndexNo + firstIndexLine), Type.Missing).Value = lv1;
indexSheet.get_Range(string.Format("C{0}", nextIndexNo + firstIndexLine), Type.Missing).Value = lv2;
indexSheet.get_Range(string.Format("D{0}", nextIndexNo + firstIndexLine), Type.Missing).Value = lv3;
Excel.Range linkRange = indexSheet.get_Range(string.Format("E{0}", nextIndexNo + firstIndexLine), Type.Missing);
linkRange.Hyperlinks.Add(linkRange, "", string.Format("'{0}'!A1", sheetName), Type.Missing, linkDescription);
nextIndexNo++;
}
Directory structure:
Could you please help me to solve this issue.
I'm about to write a WPF application that takes data from database and displays it in DataGrid. Then with a button an Excel file is created and filled with the data. With large quantities like 20000 lines the filling in Excel takes too long. Does someone have an idea why? Thank you
private void copyAlltoClipboard()
{
Clipboard.Clear();
DataGrid1.SelectAllCells();
DataGrid1.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, DataGrid1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
copyAlltoClipboard();
Excel.Application xlexcel;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
This is the method that I am using currently.
But this is much similar to your code. But I am using a data object to copy the content to clipboard. This works really fine for me. But a number of lines like 20000 will anyway affect the performances.
private void btnExportToExcel_Click(object sender, EventArgs e)
{
copyDataGridToClipboard();
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
private void copyDataGridToClipboard()
{
YourDataGridView.MultiSelect = true;
yourDataGridView.SelectAll();
DataObject dataObj = yourDataGridView.GetClipboardContent();
if (dataObj != null)
{
Clipboard.SetDataObject(dataObj);
}
yourDataGridView.MultiSelect = true;
}
I have a datagridview in my windowsforms project and I can export it to an Excel file like as:
private void btnExcel_Click(object sender, EventArgs e)
{
copyAlltoClipboard();
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Open("C:\\file.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[12, 3];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
private void copyAlltoClipboard()
{
dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
dataGridView1.MultiSelect = true;
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
I update the datagridview data by a button click. I want to copy these all of datagridview data and paste them to the same excel file sheet. Of course cell ranges should change. How can I do?
I don't use C# - here is a VB.Net version:
Use the C# version of Imports Microsoft.Office.Interop to save some typing.
In the OP the Dim CR... set the destination range. I added a second paste with a different range.
The code operates on the range directly instead of using the current selection. When you record macros the generated code operates on the selection but your code can define a range and operate on it directly.
Imports Microsoft.Office.Interop
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
copyAlltoClipboard(dgv)
Dim xlexcel As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlexcel = New Excel.Application
xlexcel.Visible = True
xlWorkBook = xlexcel.Workbooks.Open("C:\temp\file.xlsx")
xlWorkSheet = xlWorkBook.Worksheets(1)
Dim CR As Excel.Range = xlWorkSheet.Cells(12, 3)
CR.PasteSpecial()
' change range and paste again
CR = xlWorkSheet.Cells(1, 3)
CR.PasteSpecial()
End Sub
I am trying to export to excel from my Datagrid. The Datagrid has a column where we put comments in. In that column the comments are put on separate lines. When it exports to excel it exports in multiple rows.
Here is how it looks in my DataGrid:
Here is how it looks when I export to Excel:
Here is the code that I am using to do it:
public static void CreateExcelFromClipboard(string worksheetName, string fullFilePath, bool toOpen)
{
//Excel Application class
_Application app = new Microsoft.Office.Interop.Excel.Application();
//Get process id
int excelProcessId = GetApplicationProcessId(app);
try
{
//Add workbook
Workbook theWorkbook = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
//Add worksheet
var theWorksheet =
(Worksheet)theWorkbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
theWorksheet.Name = worksheetName;
app.SheetsInNewWorkbook = 1;
app.DisplayAlerts = false;
theWorksheet.Activate();
//Paste to the worksheet from clpboard
theWorksheet.Paste(Type.Missing, Type.Missing);
//Apply Borders
ApplyBorder(theWorksheet.UsedRange);
//Auto Fit All columns
Range xlRange = theWorksheet.UsedRange;
// put all hardcodes in a constant class
xlRange.Font.Name = "Arial";
xlRange.Font.Size = 9;
var firstRowRange = (Range)xlRange.Rows[1, Missing.Value];
firstRowRange.EntireRow.Font.Bold = true;
firstRowRange.EntireRow.HorizontalAlignment = XlHAlign.xlHAlignCenter;
//Set Wrap Test to false
xlRange.WrapText = true;
xlRange.Columns.AutoFit();
theWorksheet.Activate();
//Save the file
theWorkbook.SaveAs(fullFilePath, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
if (!toOpen)
{
//Clean up
app.Quit();
Marshal.ReleaseComObject(app);
}
}
catch
{
ForceExcelClose(excelProcessId);
throw;
}
finally
{
app.Visible = toOpen;
}
}
I have tried a few options but none seem to work, and some send errors.
Please let me know what I am doing wrong
public string Main(String wbPath, String wbName)
{
string cName = "";
Excel.Application xlApp;
Excel.Workbook xlWB;
Excel.Worksheet xlWS;
xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
xlApp.Calculation = Excel.XlCalculation.xlCalculationManual; //Error occurs here
xlWB = xlApp.Workbooks.Open(wbPath + wbName);
xlWB.SaveAs("vFile.html", Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml);
cName = xlWB.FullName;
xlWB.Close();
xlApp.Quit();
return cName;
}
Error code:
{"Exception from HRESULT: 0x800A03EC"}
You must open the workbook before setting the xlApp.Calculation:
static void Main(string[] args)
{
string cName = "";
var xlApp = new Application();
var xlWB = xlApp.Workbooks.Open("youpathgoeshere", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlApp.Calculation = XlCalculation.xlCalculationManual;
var xlWS = new Worksheet();
xlWB.SaveAs("vFile.html", Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml);
cName = xlWB.FullName;
xlWB.Close();
xlApp.Quit();
}
You can try this solution:
This example causes Microsoft Excel to calculate workbooks before they are saved to disk.
Excel.Application xlApp;
xlApp = new Excel.Application();
Application.Calculation = xlCalculationManual
Application.CalculateBeforeSave = True