I have created the sheet1 and populated some data in the sheet, using the data from sheet1 i want to create a chart sheet with ploting the data
try
{
app = new Excel.Application();
app.Visible = true;
workbook = app.Workbooks.Add(1);
worksheet = (Excel.Worksheet)workbook.Sheets[1];
PopulateDateInExcel(pathtologsfolder, startdate, enddate);
// create a chart
Excel.Range chartRange;
object misValue = System.Reflection.Missing.Value;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)workbook.Charts[2];
Excel.Chart chartPage = myChart.Chart;
chartRange = worksheet.get_Range("AN1", "AP6");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xl3DLine;
}
catch (Exception e)
{
//Console.Write("Error");
}
finally
{
}
Thanks in advance,
Excel Automation
Try this (UNTESTED)
Excel.ChartObject myChart = (Excel.ChartObject)charts.Add(10, 70, 250, 250);
instead of
Excel.ChartObject myChart = (Excel.ChartObject)workbook.Charts[2];
and then once your chart is created, move it to a chart sheet using this code
chart.Location(XlChartLocation.xlLocationAsNewSheet, Type.Missing);
The way to do this would be to use the Add method:
Excel.Workbook xlWorkbook;
Excel.Chart chartName = (Excel.Chart)xlWorkbook.Charts.Add();
Admittedly, at least in my experiments this causes trouble as the dataset it is using by default is fairly random (Excel magic...) so you might be needing to reformat it afterwards (delete and add datasets, etc), but that's the method to do it.
EDIT:
I should note that Excel support documentation seems to recommend the Add2 method method, but I haven't managed to use it successfully:
Excel.Workbook xlWorkbook;
Excel.Chart chartName = (Excel.Chart)xlWorkbook.Charts.Add2();
Excel.Application oXL = new Excel.Application();
and after then
oXL.Run() and see attached:
Related
In my application I have a requirement to create an excel file contains several combos.I have created upto that.
Now I have to read those combo's value from excel.
I have found a link to read from excel Read From Excel
But In my code I have found this..
Here is my code
Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook oWB;
Microsoft.Office.Interop.Excel._Worksheet oSheet;
Microsoft.Office.Interop.Excel.Range oRng;
//Get a new workbook.
oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Open("C:\\TopicUpload_2017October14.xls"));
//3rd Sheet
oSheet = (Microsoft.Office.Interop.Excel._Worksheet) oWB.Sheets.get_Item(1);
Microsoft.Office.Interop.Excel.DropDowns allDropDowns = oSheet.DropDowns(Type.Missing);
Microsoft.Office.Interop.Excel.DropDown oneDropdown = allDropDowns.Item("2");
Now how can I get selected text of this dropdown.. When I inspect I got
oneDropdown.ListCount = 5.0; // items count of second drop down, which is true
But could not able to get selected text.
oneDropdown.Text
After searching , I'm able to get it.
#region Read value from excel combobox
Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook oWB;
Microsoft.Office.Interop.Excel._Worksheet oSheet;
Microsoft.Office.Interop.Excel.Range oRng;
//Get a new workbook.
oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Open("C:\\TopicUpload_2017October14.xls"));
//3rd Sheet
oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.Sheets.get_Item(1);
Microsoft.Office.Interop.Excel.DropDowns allDropDowns = oSheet.DropDowns(Type.Missing);
Microsoft.Office.Interop.Excel.DropDown oneDropdown = allDropDowns.Item("1"); // first combo
string selectedText = oneDropdown.get_List(oneDropdown.ListIndex);
#endregion
I am writing a C# program which copies a range of cells from a worksheet of one workbook to a worksheet of an other workbook. But the problem I am facing is I am only able to copy and paste the whole worksheet of first workbook. I want to know how to select only a specific range(from row 5 [column 1 to column 10] to row 100 [column 1 to column 10]) and paste it in second workbook worksheet starting from row 2 column 8.
Also i want to know how a fill a column say from C1 to C100 with some value in a direct way instead of using the loop like below
for(i=1;i<2;i++)
{
for(j=1;j<101;i++)
{
worksheet.cells[i,j]="Fixed";
}
}
Here is the code that i have written so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Excel.Application srcxlApp;
Excel.Workbook srcworkBook;
Excel.Worksheet srcworkSheet;
Excel.Range srcrange;
Excel.Application destxlApp;
Excel.Workbook destworkBook;
Excel.Worksheet destworkSheet;
Excel.Range destrange;
string srcPath;
string destPath;
//Opening of first worksheet and copying
srcPath="C:\\Documents and Settings\\HARRY\\Desktop\\incident.csv";
srcxlApp = new Excel.Application();
srcworkBook = srcxlApp.Workbooks.Open(srcPath);
srcworkSheet = srcworkBook.Worksheets.get_Item(1);
srcrange = srcworkSheet.UsedRange;
srcrange.Copy(Type.Missing);
//opening of the second worksheet and pasting
destPath = "C:\\Documents and Settings\\HARRY\\Desktop\\FIXED Aging incident Report.xls";
destxlApp = new Excel.Application();
destworkBook = destxlApp.Workbooks.Open(destPath,0,false);
destworkSheet = destworkBook.Worksheets.get_Item(1);
destrange = destworkSheet.Cells[1, 1];
destrange.Select();
destworkSheet.Paste(Type.Missing, Type.Missing);
destworkBook.SaveAs("C:\\Documents and Settings\\HARRY\\Desktop\\FIXED Aging incident Report " + DateTime.Now.ToString("MM_dd_yyyy") + ".xls");
srcxlApp.Application.DisplayAlerts = false;
destxlApp.Application.DisplayAlerts = false;
destworkBook.Close(true, null, null);
destxlApp.Quit();
srcworkBook.Close(false, null, null);
srcxlApp.Quit();
}
}
}
You should be able to do this:
Excel.Range from = srcworkSheet.Range("C1:C100");
Excel.Range to = destworkSheet.Range("C1:C100");
from.Copy(to);
mrtig has a very elegant solution. But it won't work if you have the workbooks in separate instances of excel. So, the key is to open them in just one instance. I've modified your example to show using this approach:
public void CopyRanges()
{
// only one instance of excel
Excel.Application excelApplication = new Excel.Application();
srcPath="C:\\Documents and Settings\\HARRY\\Desktop\\incident.csv";
Excel.Workbook srcworkBook = excelApplication.Workbooks.Open(srcPath);
Excel.Worksheet srcworkSheet = srcworkBook.Worksheets.get_Item(1);
destPath = "C:\\Documents and Settings\\HARRY\\Desktop\\FIXED Aging incident Report.xls";
Excel.Workbook destworkBook = excelApplication.Workbooks.Open(destPath,0,false);
Excel.Worksheet destworkSheet = destworkBook.Worksheets.get_Item(1);
Excel.Range from = srcworkSheet.Range("C1:C100");
Excel.Range to = destworkSheet.Range("C1:C100");
// if you use 2 instances of excel, this will not work
from.Copy(to);
destworkBook.SaveAs("C:\\Documents and Settings\\HARRY\\Desktop\\FIXED Aging incident Report " + DateTime.Now.ToString("MM_dd_yyyy") + ".xls");
srcxlApp.Application.DisplayAlerts = false;
destxlApp.Application.DisplayAlerts = false;
destworkBook.Close(true, null, null);
srcworkBook.Close(false, null, null);
excelApplication.Quit();
}
For the First part of setting the same value for the entire range, instead of looping following will work out
range1 = workSheet.get_Range("A1:B100");
range1.Value = "Fixed";
And for copying you can try what #mrtig has suggested.
When exporting to excel I get blank or multiple rows per data source row when one of my data source cells contains a line break (Carriage Return). I want to prevent this.
This is the code I am using to create a workbook and paste in my data:
//Copy to clipboard
DGVJobList.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
DGVJobList.SelectAll();
DataObject DO = DGVJobList.GetClipboardContent();
Clipboard.SetDataObject(DO);
DGVJobList.ClearSelection();
//Create Excel Workbook and Paste
Excel.Application XLApp = new Excel.Application();
Excel.Workbook XLWorkBook;
Excel.Worksheet XLWorkSheet;
object misValue = System.Reflection.Missing.Value;
XLWorkBook = XLApp.Workbooks.Add(misValue);
XLWorkSheet = (Excel.Worksheet)XLWorkBook.Worksheets.get_Item(1);
Excel.Range XLRange = (Excel.Range)XLWorkSheet.Cells[1, 1];
XLRange.Select();
XLWorkSheet.PasteSpecial(XLRange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
Instead of using the clipboard, you will have more control over data placement by creating a 2D array of data out of your DGVJobList. Then, create a reference to the Excel Range of the same dimensions and set its value. This example shows only a generic 2D array for illustration. You would build your 2D array to lay out your data the way you want.
//Build data as 2D array
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
//Create Excel Workbook and Paste
Excel.Application XLApp = new Excel.Application();
Excel.Workbook XLWorkBook;
Excel.Worksheet XLWorkSheet;
object misValue = System.Reflection.Missing.Value;
XLWorkBook = XLApp.Workbooks.Add(misValue);
XLWorkSheet = (Excel.Worksheet)XLWorkBook.Worksheets.get_Item(1);
//Use range same size as data
Excel.Range XLRange = (Excel.Range) XLWorkSheet.Range["A1", "B4"];
XLRange.Value = array2D;
I am trying to add two series to an Excel chart. I am able to add one series to the chart with this code. How can i add one more series to my chart?
worksheet.Select(Type.Missing);
Excel.Range chartRange;
object misValue = System.Reflection.Missing.Value;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = worksheet.get_Range("B2", "B10");
chartPage.SetSourceData(chartRange, Excel.XlRowCol.xlColumns);
chartPage.ChartType = Excel.XlChartType.xlLine;
var series = (Excel.Series)chartPage.SeriesCollection(1);
series.Values = chartRange;
chartRange = worksheet.get_Range("A2", "A10");
series.XValues = chartRange;
Don't know the C# for it, but in VBA I would use something like:
With ChartPage.SeriesCollection.NewSeries
.Values = ActiveSheet.Range("C2:C10")
End With
I am currently working in a C# application which has a class which will generate an excel file. Everything went smooth. The data populated on the excel sheet has 'Times New Roman' has font. I would like to change it to some other fonts (Calibari). How can I do that programmatically.
From what I tried, simply changing font name, size etc... on range changes font for that range:
range.Font.Name = "Arial"
range.Font.Size = 10
range.Font.Bold = true
Here is how:
//Declare Excel Interop variables
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
//Initialize variables
xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//Set global attributes
xlApp.StandardFont = "Arial Narrow";
xlApp.StandardFontSize = 10;
Focus on the 2nd line from the bottom. That sets the default font type, but I wanted to show you where xlApp came from, even if it's self explanatory.
the following worked for me, when I tried setting the default application font it did nothing so I was able to set the font name of the active sheet rows and it worked. Also worth noting I used and tested this using Excel Interop version 12
Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//Create\Add workbook object
Excel.Workbooks workBooks = excelApp.Workbooks;
//Excel.Workbook
Excel.Workbook workBook = workBooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
//use worksheet object
Excel.Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
//set default font
workSheet.Rows.Font.Name = "Arial";
Have you tried something like this:
new Font("Arial", 10, FontStyle.Bold);
var range = worksheet.get_Range(string.Format("{0}:{0}", startRowIndex, Type.Missing));
range = range.EntireRow;
range.Style.Font.Name = "Arial";
range.Style.Font.Bold = false;
range.Style.Font.Size = 12;
Hey Do not upset I do it and works for me .
Just define Font.Name and excell sheet fill all sheet use everywhere .
Any Way Code is :
workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount-1]].Merge();
workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount - 1]].Interior.Color = ColorTranslator.ToOle(Color.FromArgb(23,65,59));
workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount - 1]].Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount - 1]].Style.Font.Name = "Arial Narrow";
((Excel.Range)WorksheetResult.UsedRange).Font.Name = "Avant Garde";
WorksheetResult is just a sheet reference.
Found this thread by my own similar problem. I had a little picker box that, when a cell was clicked, needed to paste a unique font's symbol into the selected excel cell. Here's how i did that:
string selectedItem = arrayOfSymbols[tableLayoutPanel1.GetRow((Control)sender), tableLayoutPanel1.GetColumn((Control)sender)];
Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range cell = Globals.ThisAddIn.Application.ActiveCell;
ws.Cells[cell.Row, cell.Column].Font.Name = "My Custom Font";
ws.Cells[cell.Row, cell.Column] = selectedItem;