How to add a new row at the beginning of a existing excel sheet using exceljs - node.js

It adds at the end of the sheet. I want to add at the beginning. How can I do it using exceljs
workbook.xlsx.readFile(nameFileExcel)
.then(function() {
var worksheet = workbook.getWorksheet(2);
var lastRow = worksheet.lastRow;
var getRowInsert = worksheet.getRow(++(lastRow.number));
getRowInsert.getCell('A').value = 'yo';
getRowInsert.commit();
return workbook.xlsx.writeFile(nameFileExcel);
});

Related

Adding a second sheet & saving it doesn't show in Excel

I have 2 datable and I added for loop to create the sheet and save it, but when I open excel file I can see only one(1st) sheet.
Please check why 2nd is not added in excel.
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook wb = xlApp.Worksheets.Add();
Excel.Worksheet ws = null;
Excel.Range chartRange =null;
for(int i=0; i<dataset.Tables.Count; i++)
{
// here I am creating new sheet for each datatable but only 1st datatable value I can see in
XL, 2nd sheet is not created
ws = (Excel.Worksheet)worksheets.Add();
ws.Name = dataset.Table[i].TableName;
//business logic here to assign data
var dgarray = new object[rows.count, columns.count];
//for loop to assign column name
dgarray[0,c] = column name;
//for loop to assign row value
dgarray[rowindex, colindex] = row value;
int rowcount = dgarray.getlength(0);
int columscount = dgarray.getlength(1);
chartRange = (Excel.Range)ws.cells[1,1];
chartRange = chartRange.get_Resize(rowcount,colcount);
chartRange.set_Value(Excel.XlRangeValueDatatype.XlRangeValueDefault,dgarray);
}
((Excel.Worksheet).ws).SaveAs(filename);

How to keep the background color when you edit the excel sheet by POI

I code in C#. I use NPOI(version 2.5.5). But I link this OP to Apache POI as well because the solution for this would be the same for both modules.
I have this excel file whose extention is .xlsx:
I editted the Excel sheet via NPOI.
code:
private void button_editByNpoi_Click(object sender, EventArgs e)
{
string filePath = destFileName;
IWorkbook workbook;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
workbook = new XSSFWorkbook(fs);
}
/*
//1枚目のシートを取得
ISheet sheet = workbook.GetSheetAt(0);
//セルを取得
IRow row = sheet.GetRow(1);
ICell cell = row.GetCell(1);
//セルに書き込み
//cell.SetCellValue("a");
*/
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
//上書き保存する。
workbook.Write(fs);
}
}
As the result, I got this excel sheet:
the light gray color has gone after editting. How can I keep the color?

Creating a loop to rename all sheets in workbook using excel office script

This is the code:
/**
* This sample renames a worksheet from "Sheet1" to "SALES".
*/
function main(workbook: ExcelScript.Workbook) {
// Get a worksheet named "Sheet1".
const sheet = workbook.getWorksheet('Sheet1');
// Set its name to SALES.
sheet.setName('SALES');
// Get a worksheet named "SALES".
//const sheet = workbook.getWorksheet('SALES');
// Position the worksheet at the beginning of the workbook.
sheet.setPosition(0);
}
You can use this code to rename all of the sheets in the file using a convention like sales01, sales02, etc.
function main(workbook: ExcelScript.Workbook) {
let worksheets : ExcelScript.Worksheet[] = workbook.getWorksheets()
let sheetsCount: number = worksheets.length
for (let i = 0; i < sheetsCount; i++) {
worksheets[i].setName(`SALES0${i + 1}`)
}
}

Emulate simple VBA macro to be compatible with Google Sheets

How can I convert this simple VBA to Google Sheets?
Sub SELECT()
Dim Cval As Variant
Cval = Sheet4.Range("A16").Value
Sheet4.Range("D1:T" & Cval).Select
End Sub
I keep getting errors while trying to use this macro in Google Sheets and I cannot find the issue.
The code I'm currently working on in Google Sheets:
function SELECTIE() {
var spreadsheet = SpreadsheetApp.getActive();
var Cval = spreadsheet.getRange("A16").Value
spreadsheet.getRange("D1:T" & Cval).activate();
};
You only have one minor issue.
In the first statement, you're getting the spreadsheet object, but you need the sheet object. So getSheets()[0] will get you the first worksheet.
function SELECTIE() {
var ss = SpreadsheetApp.getActive().getSheets()[0];
var cVal = ss.getRange('A16').getValue();
ss.getRange('D1:T' + cVal).activate();
}

how to write data into the excel sheet without erasing the previous data?

I tried the following code but not work properly.Any type of suggestion will be appreciated
public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException {
InputStream inp = new FileInputStream("C:\\Documents and Settings\\Administrator\\Desktop\\Read Data.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(1);
Cell cell = row.getCell(0);
cell.setCellType(cell.CELL_TYPE_STRING);
String cellContents = cell.getStringCellValue();
//Modify the cellContents here
// Write the output to a file
//cell.setCellValue(cellContents);
FileOutputStream fileOut = new FileOutputStream("C:\\Documents and Settings\\Administrator\\Desktop\\Read Data.xlsx");
sheet.createRow(1).createCell(1).setCellValue(cellContents);
wb.write(fileOut);
fileOut.close();
System.out.println(cellContents);
}
You can try this
InputStream inp = new FileInputStream("wb.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt([sheet index]);
Row row = sheet.getRow([row index]);
Cell cell = row.getCell([cell index]);
String cellContents = cell.getStringCellValue();
//Modify the cellContents here
// Write the output to a file
cell.setCellValue(cellContents);
FileOutputStream fileOut = new FileOutputStream("wb.xls");
wb.write(fileOut);
fileOut.close();
Your problem is you are re-creating an existing row, which is wiping out the values already there:
Row row = sheet.getRow(1);
....
sheet.createRow(1).createCell(1).setCellValue(cellContents);
The first call fetches the 2nd row, the second one wipes it and creates a new one
All you need to do is change the second one to re-use the already fetched existing row, and you'll keep the existing values!
Something like (with other fixes....):
DataFormatter formatter = new DataFormatter();
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(1);
Cell cell = row.getCell(0);
String cellContents = formatter.formatCellValue(cell);
//Modify the cellContents here
row.createCell(1).setCellValue(cellContents);
FileOutputStream fileOut = new FileOutputStream("C:\\Documents and Settings\\Administrator\\Desktop\\Read Data.xlsx");
wb.write(fileOut);
fileOut.close();

Resources