How to create readonly Excel sheet using C# .net - c#

I am creating a dynamic Excel sheet using ExcelWorksheet. I need to create a non-editable excel. ws.Cells["A1:Q12"].Style.Locked = true is not working.
Here is my code :
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Server.MapPath("~/Download/Sample.xlsx");
using (ExcelPackage pck = new ExcelPackage())
{
FileInfo summaryFilePath = new FileInfo(filePath);
ExcelWorksheet ws= pck.Workbook.Worksheets.Add("Sample Page");
CreateForamters(ws);
}
}
private void CreateForamters(ExcelWorksheet ws)
{
ws.Cells["B8:L8"].Value = "SamplePage";
ws.Cells["B10:L10"].Value = DateTime.Now.ToString("MMM-yy");
ws.Cells["B11:L11"].Value = "test data........-";
ws.Cells["B8:L11"].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells["B8:L11"].Style.Font.Bold = true;
ws.Cells["B8:L11"].Style.Font.Name = "Arial";
ws.Cells["B8:L11"].Style.Font.Size = 16;
ws.Cells["B8:L11"].Style.Font.Color.SetColor(Color.Blue);
ws.Cells["B8:L11"].Style.Fill.BackgroundColor.SetColor(Color.White);
ws.Cells["B8:L11"].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
ws.Cells["B8:L8"].Merge = true;
ws.Cells["B9:L9"].Merge = true;
ws.Cells["B10:L10"].Merge = true;
ws.Cells["B11:L11"].Merge = true;
ws.Cells["A1:Q12"].Style.Locked = true;
}
Thank you all in advance for your response.

I am creating a dynamic Excel sheet using ExcelWorksheet. I need to create a non-editable excel. ws.Cells["A1:Q12"].Style.Locked = true is not working.
To create NON-Editable Cells, you have to use
ws.get_Range("A1", "Q12").Locked = true;
And then you need to protect the worksheet. Without protecting the worksheet, the .Locked command doesn't have any significance.
Here is a basic example (TRIED AND TESTED IN VS2010 + OFFICE 2010)
object misValue = System.Reflection.Missing.Value;
ws.get_Range("A1", "Q12").Locked = true;
string Password = "Sid";
ws.Protect(Password, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue);
NOTE: By default all cells in Excel are locked. If you don't want to protect the rest of the cells in the sheet then remember to set their .Locked property to False.
ws.Cells.Locked = false;
and then use the above code.

if you want to save Excel WorkBook as ReadOnly Save as Below:
object misValue = System.Reflection.Missing.Value;
ExcelWorkBook.ActiveWorkbook.SaveAs(save_path, Excel.XlFileFormat.xlWorkbookNormal, misValue , misValue , True, True,XlSaveAsAccessMode.xlShared, false, false, misValue, misValue , misValue );

Related

Export Chart to Excel File

I am new in C# Programming. I am creating a winform application in which I am using System.Windows.Forms.DataVisualization Chart. This chart contains multiple series. I want to export that chart data to Excel file.
To Add Data to chart I am using
chart.Series[mSeries].Points.AddXY(dt, avgData);
dt is current DateTime and the data.
So my Excel file look like
First Column is Series Name, second is DateTime and third column contain data.
So, can anyone please tell me how I can do this.
Thanks in Advance
You can refer to the following solution. First you will need to add reference to Microsoft Excel Object Library of COM. See this link on how to add. Then next step is pretty simple. You will need to add the code from the above link and replace the data which you fed in Excel with your custom data.
Here is the code shown in the link.
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
for (int i = 0; i < chart1.Series.Count; i++)
{
xlWorkSheet.Cells[1, 1] = "";
xlWorkSheet.Cells[1, 2] = "DateTime";//put your column heading here
xlWorkSheet.Cells[1, 3] = "Data";// put your column heading here
for (int j = 0; j < chart1.Series[i].Points.Count; j++)
{
xlWorkSheet.Cells[j + 2 , 2] = chart1.Series[i].Points[j].XValue;
xlWorkSheet.Cells[j + 2 , 3] = chart1.Series[i].Points[j].YValues[0];
}
}
Excel.Range chartRange;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("B2", "c5");//update the range here
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
Simply replace the cells and data from the code with your custom data and viola when you click on export button it will export it for you.

How to append to an existing Excel file without overwriting it using C#? [duplicate]

I'm fairly new with C# and I am trying to export some data from a DataGridView in C# into an Excel file. The inputs from the datagridview are filled in by the user.
Currently, my program can create an excel file along with the values from the datagridview with the given date as its file name.
My problem is I can't seem to find a way to append the data from the gridview IF the excel file already exists, it overwrites the current excel file instead.
Any help/tips/suggestion is highly appreciated.
Thanks :)
Here is my code:
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
Microsoft.Office.Interop.Excel.Sheets xlBigSheet;
Microsoft.Office.Interop.Excel.Sheets xlSheet;
object misValue;
String newPath;
private void buttonOK_Click(object sender, EventArgs e)
{
createXLSfile();
}
private void createXLSfile(){
String cDate = datePicker.Value.ToShortDateString();
String cMonth = datePicker.Value.ToString("MMMM");
String cYear = datePicker.Value.ToString("yy");
String cDay = datePicker.Value.ToString("dd");
String fName = cDay + "-" + cMonth+ "-" + cYear + ".xls";
String mainPath = #"C:\Users\User1\Desktop\" + cYear;
String folderPath = System.IO.Path.Combine(mainPath, cMonth);
String excelPath = System.IO.Path.Combine(folderPath, fName);
System.IO.Directory.CreateDirectory(mainPath);
System.IO.Directory.CreateDirectory(folderPath);
String fNameOnly = Path.GetFileNameWithoutExtension(excelPath);
String extension = Path.GetExtension(excelPath);
String path = Path.GetDirectoryName(excelPath);
newPath = excelPath;
if(File.Exists(newPath))
{
existingFile();
}else
{
newFile();
}
MessageBox.Show("Submitted");
}
private void newFile()
{
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = true;
misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet = xlWorkBook.Sheets["Sheet1"];
xlWorkSheet = xlWorkBook.ActiveSheet;
xlWorkSheet.Name = "Sheet1";
xlWorkSheet.Cells[2, 1] = "Header1";
xlWorkSheet.Cells[2, 2] = "Header2";
xlWorkSheet.Cells[2, 3] = "Total";
getData();
xlWorkBook.SaveAs(newFullPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue,
misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}
private void existingFile()
{
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = true;
xlWorkBook = xlApp.Workbooks.Open(newPath, 0,
false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"", true, false, 0, true, false, false);
xlBigSheet = xlWorkBook.Worksheets;
string x = "Sheet1";
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBigSheet.get_Item(x);
getData();
xlWorkBook.SaveAs(newPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
misValue, misValue, misValue,
misValue, misValue);
xlWorkBook.Close(misValue, misValue, misValue);
xlWorkBook = null;
xlApp.Quit();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
private void getData()
{
double a,b,c,d,total = 0;
int lastRow_ = 3;
foreach(DataGridViewRow r in dataGridView1.Rows)
{
if(!r.IsNewRow)
{
a = Convert.ToDouble(r.Cells[2].Value);
b = Convert.ToDouble(r.Cells[3].Value);
c = Convert.ToDouble(r.Cells[4].Value);
d = Convert.ToDouble(r.Cells[5].Value);
total = a + b + c + d;
xlWorkSheet.Cells[lastRow_, 1] = "Hi";
xlWorkSheet.Cells[lastRow_, 2] = "Hello";
xlWorkSheet.Cells[lastRow_, 3] = Convert.ToString(total);
lastRow_ = xlWorkSheet.Cells.Find(
"*",
xlWorkSheet.Cells[1, 1],
misValue,
Microsoft.Office.Interop.Excel.XlLookAt.xlPart,
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious,
misValue,
misValue,
misValue).Row + 1;
}
}
total = 0;
}
Update 1:
Still stuck.
Been trying to follow this link:
https://www.codeproject.com/articles/5123/opening-and-navigating-excel-with-c
OUTPUT
Directory of outputted excel file
This is what's inside the outputted excel file
When you need to append data to an existing worksheet, you need to find out where the last used row is and start adding data after this row. Your current code to get this “last” row is awkward as once you start adding rows you keep checking for this “last” row which is unnecessary. The getData() method is simply adding data to a new excel file where the last row won’t matter. If the file exists, then you simply need to get the last used row and start importing the data on the next row. I am guessing it may be better as your code goes, to send over a starting row index for the GetData(RowToStart) method and simply increment the lastRow_ variable, like below: There is no need to keep checking for this last row.
private void getData(int lastRow_) {
double a, b, c, d, total = 0;
//int lastRow_ = 4;
foreach (DataGridViewRow r in dataGridView1.Rows) {
//if (!row.IsNewRow) {
if (!r.IsNewRow) {
a = Convert.ToDouble(r.Cells[2].Value);
b = Convert.ToDouble(r.Cells[3].Value);
c = Convert.ToDouble(r.Cells[4].Value);
d = Convert.ToDouble(r.Cells[5].Value);
total = a + b + c + d;
xlWorkSheet.Cells[lastRow_, 1] = "Hi";
xlWorkSheet.Cells[lastRow_, 2] = "Hello";
xlWorkSheet.Cells[lastRow_, 3] = Convert.ToString(total);
lastRow_++;
//lastRow_ = xlWorkSheet.Cells.Find(
// "*",
// xlWorkSheet.Cells[1, 1],
// misValue,
// Microsoft.Office.Interop.Excel.XlLookAt.xlPart,
// Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
// Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious,
// misValue,
// misValue,
// misValue).Row + 1;
}
}
total = 0;
}
If the file is new, you would call this method like below.
.
.
.
xlWorkSheet.Cells[3, 1] = "Header1";
xlWorkSheet.Cells[3, 2] = "Header2";
xlWorkSheet.Cells[3, 3] = "Total";
getData(4);
.
.
.
If the file already exists and you need to append the data to existing worksheet you need to get the last used row then start on the next row. You can call getData(RowToStart) like below.
.
.
.
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBigSheet.get_Item("Sheet1");
Microsoft.Office.Interop.Excel.Range last = xlWorkSheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
int lastUsedRow = last.Row;
getData(lastUsedRow + 1);
.
.
.
I hope this makes sense.

How to represent a property value in excel

My code works fine with generating an excel file, thanks to this community here.
But I still have a problem:
I have a ListBox from which I export a data to an excel file.
When I run it and the file will be created, I open it and in the rows I see
"SheduleMenager.Employees"
Employees is the class in my project in which i have two properties:
string Name and bool Experience
Here is a button to create and fill up the excel file :
private void Generateexcelfile_Click(object sender, RoutedEventArgs e)
{
Excel.Application xlApp = new Microsoft.Office.Interop.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);
for (int i = 0; i < MonBox.Items.Count; i++)
{
xlWorkSheet.Cells[i + 1, 1] = MonBox.Items[i].ToString();
}
xlWorkBook.SaveAs("FILEPATH", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
}
I would be really happy if you can help me to represent a VALUE of Employees.Name
I'm guessing that MonBox contains list of employees, in Items collection.
If that is so, change your line
xlWorkSheet.Cells[i + 1, 1] = MonBox.Items[i].ToString();
into
xlWorkSheet.Cells[i + 1, 1] = (MonBox.Items[i] as SheduleMenager.Employees).Name;

Change the x axis values of graph c#

I am a little new to c# and Visual Studio and I am trying to plot a graph based on the values of a csv file located somewhere else. I have used the ChartObjects and ChartWizard property in c# to create the graph. The graph plotted should be the column range I am providing, in the Y-axis and X-axis should have the current row number(1,2,3,4 etc). However my graph by default takes the X-axis to be the first column in my csv file. It plots properly if I specify a range for X-axis too but how can I get the current row number there?
I went through a lot of articles and questions even on Stack Overflow but none seemed to help.
Here's a snippet of my code:
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();
var xlWorkBooks = xlexcel.Workbooks;
xlexcel.Visible = false;
xlWorkBooks.OpenText(#"C:\" + processName + ".csv", misValue, misValue, Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited, Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
// Set Sheet 1 as the sheet you want to work with
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBooks[1].Worksheets.get_Item(1);
xlWorkSheet.Shapes.AddChart(misValue, misValue, misValue, misValue, misValue).Select();
//~~> Make it a Line Chart
xlexcel.ActiveChart.ApplyCustomType(Microsoft.Office.Interop.Excel.XlChartType.xlLine);
//~~> Set the data range
xlexcel.ActiveChart.SetSourceData(xlWorkSheet.Range["E2:E200"]);
xlexcel.ActiveChart.ChartWizard(misValue, Title: chartName + " (" + processName + ")", CategoryTitle: "Iterations", ValueTitle: processType);
Microsoft.Office.Interop.Excel.ChartObjects chartObjects =(Microsoft.Office.Interop.Excel.ChartObjects)(xlWorkSheet.ChartObjects(Type.Missing));
foreach (Microsoft.Office.Interop.Excel.ChartObject co in chartObjects)
{
co.Select();
Microsoft.Office.Interop.Excel.Chart chart = (Microsoft.Office.Interop.Excel.Chart)co.Chart;
chart.Export(ConfigurationManager.AppSettings.Get("Charts") + "\\ProcessFiles" + #"\" + chartName + " (" + processName + "of" + processType + ")" + ".png", "PNG", false);
co.Delete();
}
xlWorkBooks[1].Close(true, misValue, misValue);
xlexcel.Quit();
Any guidance would be greatly appreciated.
Thanks!
I tried your code and made some modifications, I hope this will work
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
//string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string fileName = "" + "YOUR_PATH" + "\\Templates\\myCSV.csv";
string processName = "test";
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(fileName);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Shapes.AddChart(misValue, misValue, misValue, misValue, misValue).Select();
//~~> Make it a Line Chart
xlApp.ActiveChart.ApplyCustomType(Microsoft.Office.Interop.Excel.XlChartType.xlLine);
//~~> Set the data range
xlApp.ActiveChart.SetSourceData(xlWorkSheet.Range["B1:B30"]);
string chartName = "TEST CHART", processType="TEST TYPE";
xlApp.ActiveChart.ChartWizard(misValue, Title: chartName + " (" + processName + ")", CategoryTitle: "Iterations", ValueTitle: processType);
Excel.ChartObjects chartObjects = (Excel.ChartObjects)(xlWorkSheet.ChartObjects(Type.Missing));
foreach (Excel.ChartObject co in chartObjects)
{
co.Select();
Excel.Chart chart = (Excel.Chart)co.Chart;
chart.Export("C:\\YOUR_PATH" + #"\" + chart.Name + ".png", "PNG", false);
}
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
xlWorkSheet = null;
xlWorkBook = null;
xlApp = null;
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
releaseObject(xlApp);
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show(ex.Message);
}
finally
{
GC.Collect();
}
}

How to pass data from C# to an excel existing file

I want to make a button which you press for passing data from 3 textboxes to an excel table but I want to use the same table every time and just add data to a new row. Every time I want to add something, it creates me a new excel file. Here's my code.
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);
xlWorkSheet.Cells[1, 1] = "Book name";
xlWorkSheet.Cells[1, 2] = "Author";
xlWorkSheet.Cells[1, 3] = "Rating";
string curFile = #"g:\Biblioteca.xls";
if (!File.Exists(curFile))
{
xlWorkBook.SaveAs("g:\\Biblioteca.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkSheet.Cells[++k, 1] = getName;
xlWorkSheet.Cells[k, 2] = getAuthor;
xlWorkSheet.Cells[k, 3] = getRating;
MessageBox.Show("Excel created succesfuly");
}
else
{
Excel.Application excelApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(curFile,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
xlWorkSheet.Cells[++k, 1] = getName;
xlWorkSheet.Cells[k, 2] = getAuthor;
xlWorkSheet.Cells[k, 3] = getRating;
}
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
}
}
You are not opening any existing file!
See #gmiley's second reference for an example of opening an existing spreadsheet. You may need to study Range to work out how to place your new data.

Categories