I want to use C# to get the values of the checkboxes from the Excel Filter.
so ...
Proj_1 should be true
Proj_2 should be true
Proj_3 should be false
Proj_4 should be false
Below is the code I am currently using but item.Visible is ALWAYS false no matter if the UI has a check by it or not (unless I uncheck Select Multiple Items) but I need that checked
foreach (PivotField fld in pivotFields)
{
Console.WriteLine(fld.Name + " -- " + fld.Orientation + " -- " + fld.EnableItemSelection + " -- " + fld.EnableMultiplePageItems + " -- ");
foreach (PivotItem item in fld.PivotItems(Type.Missing))
{
if (item.Visible == true)
{
Console.WriteLine("Item" + item.Name + " in field " + fld.Name + "is Visible (Selected)");
}
else
{
Console.WriteLine("Item" + item.Name + " in field " + fld.Name + "is Hidden (Not Selected)");
}
}
}
This is something that works, if the Microsoft.Office.Interop.Excel library is added:
namespace ExcelAtSomething
{
using System;
using Excel = Microsoft.Office.Interop.Excel;
class Startup
{
static void Main()
{
string filePath = #"C:\Users\stackoverflow\Desktop\Sample.xlsx";
Excel.Application excel = new Excel.Application();
excel.Visible = true;
excel.EnableAnimations = true;
Excel.Workbook wkb = Open(excel, filePath);
foreach (Excel.Worksheet xlWorksheet in wkb.Worksheets)
{
Excel.PivotTables pivotTablesCollection = xlWorksheet.PivotTables();
if (pivotTablesCollection.Count > 0)
{
for (int i = 1; i <= pivotTablesCollection.Count; i++)
{
Excel.PivotTable currentPivotTable = pivotTablesCollection.Item(i);
Console.WriteLine($"Table is named -> {currentPivotTable.Name}");
foreach (Excel.PivotField pivotField in currentPivotTable.PivotFields())
{
Console.WriteLine($"\nField is named -> {pivotField.Name}");
foreach (Excel.PivotItem visibleItems in pivotField.VisibleItems)
{
Console.WriteLine($"Visible item name -> {visibleItems.Name}");
}
foreach (Excel.PivotItem PivotItem in pivotField.PivotItems())
{
Console.WriteLine($"Item is named -> {PivotItem.Name}");
Console.WriteLine(PivotItem.Visible);
}
}
}
}
}
excel.EnableAnimations = true;
wkb.Close(true);
excel.Quit();
Console.WriteLine("Finished!");
}
private static Excel.Workbook Open(Excel.Application excelInstance,
string fileName, bool readOnly = false,
bool editable = true, bool updateLinks = true)
{
Excel.Workbook book = excelInstance.Workbooks.Open(
fileName, updateLinks, readOnly,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return book;
}
}
}
The trick is that except for PivotItem, there is a PivotField, which has a list of its visible items. If you display these, then only the visible ones are displayed:
Playing a bit and probably making custom a function, that returns the non-visible items, which are not in that list is a good option for the Excel.
Related
Brief details:
Running Office 360 with Excel 64bit installed.
Application is built on .NET 4.6 and CPU type is set to x64
The code that's not working - line marked where it fails
public bool CreateInvoice()
{
try
{
//Write file
var setting = new Classes.Settings();
string WritePath = setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID + "\\" + InvoiceDetails.InvoiceNumber + ".xlsx";
if (!System.IO.Directory.Exists(setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID))
System.IO.Directory.CreateDirectory(setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID);
if (System.IO.File.Exists(WritePath))
System.IO.File.Delete(WritePath);
System.IO.File.WriteAllBytes(WritePath, AccountManagement.Properties.Resources.Invoice);
//Prepare Excel
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks xlWorkBooks = null;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook = null;
Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
Microsoft.Office.Interop.Excel.Worksheet xlExportSheet = null;
Classes.GetExcelProcessID getExcelProcessID = new GetExcelProcessID();
xlWorkBooks = xlApp.Workbooks; //***********FAILS HERE*************
xlWorkBook = xlWorkBooks.Add(WritePath);
xlSheets = xlApp.Worksheets;
xlExportSheet = (Microsoft.Office.Interop.Excel.Worksheet)(xlSheets[1]);
var cells = xlExportSheet.Cells;
var Process = Classes.GetExcelProcessID.ExcelProcesID(xlApp);
try
{
xlApp.Visible = false;
xlApp.DisplayAlerts = false;
//This is where I make my edits.
xlWorkBook.SaveAs(WritePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
xlWorkBook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID + "\\" + InvoiceDetails.InvoiceNumber + ".pdf");
InvoiceSavePath = setting.DefaultSavePath + "\\Invoices\\" + InvoiceDetails.CustomerID + "\\" + InvoiceDetails.InvoiceNumber + ".pdf";
InvoiceDetails.InvoiceSavePath = InvoiceSavePath;
}
catch (Exception ex)
{
return false;
}
finally
{
//Ensue all COM objects are closed and realesed or EXCEl will remain open.
xlWorkBook.Close(0, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
xlWorkBooks.Close();
//xlApp.Application.Quit();
xlApp.Quit();
while (Marshal.FinalReleaseComObject(xlApp) != 0) { }
while (Marshal.FinalReleaseComObject(xlWorkBooks) != 0) { }
while (Marshal.FinalReleaseComObject(xlWorkBook) != 0) { }
while (Marshal.FinalReleaseComObject(xlSheets) != 0) { }
while (Marshal.FinalReleaseComObject(xlExportSheet) != 0) { }
while (Marshal.FinalReleaseComObject(cells) != 0) { }
xlApp = null;
xlWorkBooks = null;
xlWorkBook = null;
xlSheets = null;
xlExportSheet = null;
cells = null;
try
{
Process.Kill();
}
catch
{
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
This is the exception I keep getting.
Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'
I initially thought the issue was about my application possibly still being built at x86 but I have a interop function with Word that runs perfectly fine - and that's 64bit.
I've tried google but all the fixes are to do with 64bit office and a 32bit application.
Any tips?
Is there any way to convert MSProject.Project that I am reading from an .mpp file to a DataSet?
Or it is impossible is the first time I use this tool from MSProject.Project
public new string Load(string fileName)
{
MSProject.ApplicationClass app = null;
string retVal = "";
List<MSProject.Task> tasks = new List<MSProject.Task>();
DataSet Datos = new DataSet();
try
{
// execute the Microsoft Project Application
app = new MSProject.ApplicationClass();
// Do not display Microsoft Project
app.Visible = false;
// open the project file.
if (app.FileOpen(fileName, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSProject.PjPoolOpen.pjPoolReadOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing))
{
// go through all the open projects--there should only be one
foreach (MSProject.Project proj in app.Projects)
{
// go through all the tasks in the project
foreach (MSProject.Task task in proj.Tasks)
{
// add Microsoft Project Task to arraylist
Datos.Tables.Add(task);
//tasks.Add(task);
}
}
}
else
{
retVal = "The MS Project file " + fileName + " could not be opened.";
}
}
catch (Exception ex)
{
retVal = "Could not process the MS Project file " + fileName + "." + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.StackTrace;
}
// close the application if is was opened.
if (app != null)
{
app.Quit(MSProject.PjSaveType.pjDoNotSave);
}
return retVal;
}
I already managed to pass it to a list<> but it would work better to have it in a DataSet
It is the section that I have commented here
foreach (MSProject.Task task in proj.Tasks)
{
// add Microsoft Project Task to arraylist
Datos.Tables.Add(task);
//tasks.Add(task);
}
The error is as follows:
Argument 1: cannot be converted from 'Microsoft.Office.Interop.MSProject.Task' to 'System.Data.DataTable'
so currently my code below generates a string and adds it to a list for each image file in a directory, then after its does adding to the list, it opens an excel file, appends the list, and formats it. Currently my problem is that my excel line is selecting the entireColumn and pasting the list, how can I get it to recognize how many strings are in the list and only append that many to the excel sheet.
//Fill A2:B6 with an array of values .
oRng = oSheet.get_Range("C2");
oRng.EntireColumn.Value2 = checkInformation.ToArray();
Here is my whole project
class Program
{
[STAThread]
static void Main(string[] args)
{
Microsoft.Office.Interop.Excel.Application oXL;
Microsoft.Office.Interop.Excel._Workbook oWB;
Microsoft.Office.Interop.Excel._Worksheet oSheet;
Microsoft.Office.Interop.Excel.Range oRng;
object misvalue = System.Reflection.Missing.Value;
Console.WriteLine("Initializing Check Parser Menu.....");
string dataPath = #"C:\Users\User\Desktop\Tesseract\Tesseract\tessdata";
string[] filePaths = Directory.GetFiles(#"C:\Users\User\Desktop\Tesseract\Tesseract\Check_Images", "*.png");
string checkData = "";
int checkCounter = 0;
List<string> checkInformation = new List<string>();
foreach (string filePath in filePaths)
{
checkCounter++;
Console.WriteLine("Initializing Check Parser... on Check: " + checkCounter);
using (TesseractEngine ocr = new TesseractEngine(dataPath, "eng", EngineMode.TesseractOnly))
{
using (Pix p = Pix.LoadFromFile(filePath))
{
using (Pix img = p.Scale(2, 3))
{
using (var page = ocr.Process(img))
{
string text = page.GetText();
if (text.Contains("Claim ID"))
{
int indexOfNone = text.IndexOf("NONE");
int indexOfClaimId = text.IndexOf("Claim ID");
int difference = indexOfClaimId - indexOfNone;
var dollarData = text.Substring(indexOfNone, difference);
int startingPoint = indexOfNone + (dollarData.Length - 6);
var dollarAmount = text.Substring(startingPoint, 6);
var claimIdData = text.Substring(indexOfClaimId, 14);
var claimInfoOutput = claimIdData + " Check Amount: " + dollarAmount;
Console.WriteLine(claimInfoOutput);
checkInformation.Add(claimInfoOutput);
}
else
{
Console.WriteLine("You will need to locate this check, there was an issue parsing " + "\n" + filePath);
Console.WriteLine("Press any Key to continue");
Console.ReadKey();
}
}
}
}
}
}
Console.WriteLine("Writing Data to Excel File... DONT TaCH");
try
{
//Start Excel and get Application object.
oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = true;
//Get a new workbook.
oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(""));
oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;
//Add table headers going cell by cell. //Format A1:B1 as bold, vertical alignment = center.
oSheet.Cells[1, 1] = "Claim ID";
oSheet.Cells[1, 2] = "Check Amount";
oSheet.get_Range("A1", "B1").Font.Bold = true;
oSheet.get_Range("A1", "B1").VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
// Create an array to multiple values at once.
//Fill A2:B6 with an array of values .
oRng = oSheet.get_Range("C2");
oRng.Value2 = checkInformation.ToArray();
//oSheet.get_Range("A2", "B6").Value2 = checkInformation.ToArray();
//Fill A2 with a formula(=MID(C2,9,5)
oRng = oSheet.get_Range("A2", "A50");
oRng.Formula = "=MID(C2,9,5)";
//Fill B2 with a =RIGHT(C2,6)
oRng = oSheet.get_Range("B2", "B50");
oRng.Formula = "=RIGHT(C2,6)";
//AutoFit columns A:C.
oRng = oSheet.get_Range("A1", "C1");
oRng.EntireColumn.AutoFit();
oXL.Visible = false;
oXL.UserControl = false;
oWB.SaveAs("C:\\Users\\User\\Desktop\\Tesseract\\Tesseract\\excel\\checkdata.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault,
Type.Missing, Type.Missing,
false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
oWB.Close();
;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Okay I solved it guys sorry to bug you but heres what I did.
//Fill A2:B6 with an array of values .
string cellName;
int counter = 2;
foreach (var check in checkInformation)
{
cellName = "C" + counter.ToString();
oRng = oSheet.get_Range(cellName, cellName);
oRng.Value2 = check.ToString();
++counter;
}
and basically i just need to say for each item in my list this is the first cell name append the value.tostring
Intro:
Today I have decided to make an Excel automatization task with C#. This is probably the first time I am doing something like this, thus the problems are plenty.
The task:
Pretty much, the idea is the following - I have 4 excel files in folder strPath. I have to loop through all of them and make a file called Report.xlsx in the same folder, with the information from those files.
The information, that I need is anything, below row 9. Thus, the first row to copy is row number 10. That is why, the first file I loop for is saved as Report, and the bMakeOnce value is changed. After the first file is looped and saved As, I start entering into the else condition. There I locate the last used row of the XL files and I try to copy the range into the sheetReport.
The questions:
First of all - any ideas for code improvement;
Whenever I am looping through the files I get the following picture telling me that each of the looping file is opened already.
Any good idea how to do the range copy better? Currently, I simply try to put the copied range on every 200+n line, to avoid some confusion for me.
Any idea why I do not get anything in the sheetReport, except for the first file?
The code I am using (initially, for the current goto Github below):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
class MainClass
{
static void Main()
{
string strPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), #"..\..\..\"));
string[] strFiles = Directory.GetFiles(strPath);
Excel.Application excel = null;
bool bMakeOnce = true;
int intFirstLine = 10;
int intLastColumn = 50;
int lastRow;
int lastRowReport;
Excel.Workbook wkbReport = null;
string strWkbReportPath;
int n = 0;
foreach (string strFile in strFiles)
{
Console.WriteLine(strFile);
Excel.Workbook wkb = null;
Excel.Worksheet sheet = null;
Excel.Worksheet sheetReport = null;
Excel.Range rngLast = null;
Excel.Range rngLastReport = null;
Excel.Range rngToCopy = null;
Excel.Range rngDestination = null;
excel = new Excel.Application();
excel.Visible = true;
wkb = OpenBook(excel, strFile);
if (bMakeOnce)
{
bMakeOnce = false;
strWkbReportPath = wkb.Path + "\\" + "Report.xlsx";
wkb.SaveAs(strWkbReportPath);
wkbReport = OpenBook(excel, strWkbReportPath);
}
else
{
wkb = OpenBook(excel, strFile);
sheetReport = wkbReport.Worksheets[1];
sheet = wkb.Worksheets[1];
n++;
rngLastReport = sheetReport.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
rngLast = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
rngToCopy = sheet.Range[sheet.Cells[intFirstLine, 1], sheet.Cells[rngLast.Row, intLastColumn]];
int size = rngToCopy.Rows.Count;
Console.WriteLine(size);
rngDestination = sheetReport.Range[sheetReport.Cells[200 * n, 1], sheetReport.Cells[200 * n + size, intLastColumn]];
rngToCopy.Copy(rngDestination);
//rngDestination.PasteSpecial(Excel.XlPasteType.xlPasteAll);
}
}
wkbReport.Close(false);
excel.Quit();
}
public static Excel.Workbook OpenBook(Excel.Application excelInstance, string fileName, bool readOnly = false, bool editable = true, bool updateLinks = true)
{
Excel.Workbook book = excelInstance.Workbooks.Open(
fileName, updateLinks, readOnly,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return book;
}
}
Now it works somehow, producing what I want:
using System;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
class MainClass
{
static void Main()
{
string strPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), #"..\..\..\"));
string[] strFiles = Directory.GetFiles(strPath);
Excel.Application excel = null;
bool bMakeOnce = true;
string strReportName = "Report.xlsx";
int intFirstLine = 10;
int intLastColumn = 50;
int lastRow;
int lastRowReport;
int intTotalRows;
Excel.Workbook wkbReport = null;
string strWkbReportPath;
int n = 0;
excel = new Excel.Application();
excel.Visible = true;
foreach (string strFile in strFiles)
{
if (strFile.Contains(strReportName))
{
Console.WriteLine(strReportName + " is deleted.");
File.Delete(strFile);
}
}
foreach (string strFile in strFiles)
{
if (strFile.Contains(strReportName))
{
continue;
}
Console.WriteLine(strFile);
Excel.Workbook wkb = null;
Excel.Worksheet sheet = null;
Excel.Worksheet sheetReport = null;
Excel.Range rngLastReport = null;
Excel.Range rngToCopy = null;
wkb = Open(excel, strFile);
if (bMakeOnce)
{
bMakeOnce = false;
strWkbReportPath = wkb.Path + "\\" + strReportName;
wkb.SaveAs(strWkbReportPath);
wkb.Close();
wkbReport = Open(excel, strWkbReportPath);
}
else
{
sheetReport = wkbReport.Worksheets[1];
sheet = wkb.Worksheets[1];
//lastRow = sheet.Cells[1, 3].get_End(Excel.XlDirection.xlUp).Row;
intTotalRows = sheet.Rows.Count;
lastRow = sheet.Cells[intTotalRows, 1].End(Excel.XlDirection.xlUp).Row;
lastRowReport = sheetReport.Cells[intTotalRows, 1].End(Excel.XlDirection.xlUp).Row;
//lastRowReport = sheetReport.Cells[intTotalRows, 1].get_End(Excel.XlDirection.xlUp).Row;
//lastRowReport = sheetReport.Cells[intTotalRows, intTotalRows.End[Excel.XlDirection.xlUp]].Row;
n++;
rngToCopy = sheet.Range[sheet.Cells[intFirstLine,1],sheet.Cells[lastRow, intLastColumn]];
int size = rngToCopy.Rows.Count;
rngLastReport = sheetReport.Range[sheetReport.Cells[lastRowReport+1, 1], sheetReport.Cells[lastRowReport + 1+size, intLastColumn]];
rngToCopy.Copy(rngLastReport);
wkb.Close(false);
}
}
wkbReport.Close(true);
excel.Quit();
Console.WriteLine("Finished!");
}
public static Excel.Workbook Open(Excel.Application excelInstance, string fileName, bool readOnly = false, bool editable = true, bool updateLinks = true)
{
Excel.Workbook book = excelInstance.Workbooks.Open(
fileName, updateLinks, readOnly,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return book;
}
//public static Excel.Workbook OpenBook(Excel.Application excelInstance, string fileName, bool readOnly = false, bool editable = true, bool updateLinks = true)
//{
// Excel.Workbook book = excelInstance.Workbooks.Open(
// fileName, updateLinks, readOnly,
// Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
// Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
// Type.Missing, Type.Missing);
// return book;
//}
}
Thus, I have put it codeReview here: https://codereview.stackexchange.com/questions/153054/loop-through-excel-files-and-copy-correct-range-in-a-separate-file
I started to learn programming again yesterday. As an assignment i made an API where it is possible to create Excel files, where it is possible to fill in a textbox and the text from this textbox will fill a cell in excel. Here is what i made for the assignment to create a excel file(The question comes after the code).
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult DownloadExcel(string field, int id = 0)
{
var bla = field;
List<Record> obj = new List<Record>();
obj = RecordInfo(field);
StringBuilder str = new StringBuilder();
str.Append("<table border=`" + "1px" + "`b>");
str.Append("<tr>");
str.Append("<td><b><font face=Arial Narrow size=3>Fieldname</font></b></td>");
str.Append("</tr>");
foreach (Record val in obj)
{
str.Append("<tr>");
str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.Fieldname.ToString() + "</font></td>");
str.Append("</tr>");
}
str.Append("</table>");
HttpContext.Response.AddHeader("content-disposition", "attachment; filename=Information" + DateTime.Now.Year.ToString() + ".xls");
this.Response.ContentType = "application/vnd.ms-excel";
byte[] temp = System.Text.Encoding.UTF8.GetBytes(str.ToString());
return File(temp, "application/vnd.ms-excel");
}
public List<Record> RecordInfo(string fieldname = "test")
{
List<Record> recordobj = new List<Record>();
recordobj.Add(new Record { Fieldname = fieldname });
return recordobj;
}
}
Now my question is. Is it possible to modify fields in Excel via C# and is it possible to modify a specific cell with a textbox and that the data will be changed on click (like for example, I want to adjust cell D7 from 1 to 2, so I go to my form and fill in 2 in the textbox and press submit and the specific cell will show the new number when i open Excel). I searched but couldn't find the answer. A link to how it can be made is appreciated.
Thanks in advance!
I recommend that you switch to another approach and use a "grown-up" library for the excel file creation. EPPlus is free, offers great features and is very easy to use.
Have a look at the examples: http://epplus.codeplex.com/wikipage?title=WebapplicationExample
A bit late, but I answered my own question some time ago.
For the ones who are interested, here is the code for how I fixed my problem.
public class ExcelModel
{
Application excelApp;
string newValue;
public ExcelModel(string newValue)
{
excelApp = new Application();
this.newValue = newValue;
}
public void openExcelSheet(string fileName)
{
Workbook workbook = excelApp.Workbooks.Open(fileName, 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);
Worksheet sheet = (Worksheet)workbook.Worksheets.get_Item(2);
double oldValue = sheet.get_Range("D6").get_Value();
sheet.Cells[6, 4] = newValue;
workbook.SaveAs("\\Users\\user1\\Downloads\\text3.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workbook.Close();
excelApp.Visible = true;
Workbook newWorkbook = excelApp.Workbooks.Open("\\Users\\user1\\Downloads\\text3.xlsx");
}
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult UpdateExcel(string field, int id = 0)
{
ExcelModel model = new ExcelModel(field);
string file = "\\Users\\user1\\Downloads\\Testdocument.xlsx";
model.openExcelSheet(file);
return RedirectToAction("Index");
}
public List<Record> RecordInfo(string fieldname = "test")
{
List<Record> Recordobj = new List<Record>();
Recordobj.Add(new Record { Fieldname = fieldname });
return Recordobj;
}
}