i'm trying to insert some data to my excel sheet.
My main code returns true/false statement and i need to save that statements to my Registers.xls file.
How do i read and write data from same excel. (Sorry for poor english)
static void Main(string[] args)
{
Excel.Application xlApp;
Excel.Workbook xlWorkbook;
Excel.Worksheet xlWorksheet;
Excel.Range range;
xlApp = new Excel.Application();
xlWorkbook = xlApp.Workbooks.Open(@"E:\VScode\Registers.xls");
xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);
range = xlWorksheet.UsedRange;
int cl = range.Rows.Count;
string result;
string strRegist;
for (int i = 1; i <= cl;i++)
{
strRegist = (string)(range.Cells[i,3] as Excel.Range).Value2;
result = Convert.ToString(Program.CheckRegister(strRegist));
//Insert result to my sheet
Console.WriteLine(result);
}
xlWorkbook.Close(true, null, null);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorksheet);
Marshal.ReleaseComObject(xlWorkbook);
Marshal.ReleaseComObject(xlApp);
Console.WriteLine();
Console.ReadLine();
}
You could add your data into a datatable and then convert that to an excelsheet and adding that sheet into w/e workbook you want.
using OfficeOpenXml;
using System.Data;
using System.IO;
void SaveTableAsExcelSheet(DataTable dt, string filePath)
{
var fi = new FileInfo(filePath);
using (ExcelPackage pck = new ExcelPackage(fi))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(dt.TableName);
ws.Cells["A1"].LoadFromDataTable(dt, true);
pck.Save();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.