C# Deleting rows of excel sheet that contain variable not working - excel

So below you will see the code im using to iterate through a excel spreadsheet and find every row that contains x. For some reason not only does the operation STOP at the very last row and not finish the operation, allowing me to save my edits and close the handle to excel, but also seems to be skipping rows. It will go through, say 25k rows that need to be deleted, yet only delete 12.5k. Im sure im doing something simple wrong here, so im hoping a few sets of eyes on it can spot my mistake. My code:
void startOperation()
{
Console.WriteLine("Beginning start operations..." + searchText);
Console.WriteLine("Opening Workbook: " + filePath);
// create excel-instance:
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
// open the concrete file:
Workbook xlWorkbook = xlApp.Workbooks.Open(#filePath);
//MessageBox.Show(filePath);
// select worksheet. NOT zero-based!!:
_Worksheet xlWorksheet = xlWorkbook.Sheets[1];
//MessageBox.Show(Convert.ToString(xlWorksheet.Name));
Microsoft.Office.Interop.Excel.Range range;
int numRows = xlWorksheet.UsedRange.Rows.Count;
numRowsDeleted = 0;
nullCounter = 0;
var percentageComp = new Decimal(.001);
//Parallel.For(1, numRows, new ParallelOptions { MaxDegreeOfParallelism = 1 }, i =>
for (currRow = 1; currRow <= numRows; currRow++)
{
percentageComp = ((decimal)currRow / (decimal)numRows) * 100;
Console.Clear();
Console.WriteLine("Number of Rows: " + numRows);
Console.WriteLine("Checking Row #: " + currRow);
Console.WriteLine("Number of Rows Deleted: " + numRowsDeleted);
Console.WriteLine("Percentage Comp: " + percentageComp.ToString("#.##"));
//Create Worksheet Range
range = (Microsoft.Office.Interop.Excel.Range)xlWorksheet.Cells[currRow, 2];
//MessageBox.Show(cellValue);
if (currRow == numRows)
{
closeOperation(xlApp, xlWorkbook);
break;
}
if (Convert.ToString(range.Value) != null) //if (cellValue != null || cellValue != "")
{
cellValue = Convert.ToString(range.Value);
//nullCounter = 0;
//MessageBox.Show("Cell Value: " + cellValue);
if (cellValue.Contains("MESSAGE NOT CONFIGURED"))
{
//MessageBox.Show("Cell Value: " + cellValue);
xlWorksheet.Rows[currRow].Delete(XlDeleteShiftDirection.xlShiftUp);
numRowsDeleted++;
//currRow++;
}
else
{
//currRow++;
}
}
else
{
//nullCounter++;
//currRow++;
}
//currRow++;
}
}
Again, your help is appreciated. Thanks!

Related

Split column into rows using excel office script

I would like to split a column into rows using excel office script but I cant figure out how.
I have a schedule in below format in excel which I would like to split into columns.
Original table
Final table need to be like this.
Final Table
Is this achievable, if yes, could someone please share the code
Based on your description I made an attempt at a solution with Office Scripts. It takes a table like this:
and outputs a new table on a new worksheet, like this:
For better or worse I attempted to keep the logic in the workbook via formulas derived from the first table and output into the second. This formula logic would need to be rewritten if there is more than one activity per day.
I'm not a developer but I can already see areas in this Office Script that need improvement:
function main(workbook: ExcelScript.Workbook) {
// delete new worksheet if it already exists so the rest of the script can run effectively
// should you need to retain data simply rename worksheet before running
if (workbook.getWorksheet("My New Sheet") != undefined) {
workbook.getWorksheet("My New Sheet").delete()
}
// assumes your original data is in a table
let myTable = workbook.getTable("Table1");
let tableData = myTable.getRangeBetweenHeaderAndTotal().getValues();
// extract the dates as excel serial numbers
let allDates:number[] = [];
for (let i = 0; i < tableData.length; i++) {
allDates.push(tableData[i][2], tableData[i][3]);
}
let oldestDate = Math.min(...allDates);
let newestDate = Math.max(...allDates);
let calendarSpread = newestDate-oldestDate+2;
// construct formula from the tableData
// first add a new 'column' to tableData to represent the days of the week (as numbers) on which the activity is planned. this will be an array added to each 'row'.
for (let r = 0; r < tableData.length; r++) {
tableData[r].push(findDay(tableData[r][1]));
}
// start a near blank formula string
let formulaText:string = '=';
// use the following cell reference
let cellRef = 'C2';
// construct the formual for each row in the data and with each day of the week for the row
let rowCount:number;
for (let r = 0; r < tableData.length; r++) {
if (tableData[r][4].length > 1) {
formulaText += 'IF(AND(OR(';
} else {
formulaText += 'IF(AND(';
}
for (let a=0; a < tableData[r][4].length; a++) {
formulaText += 'WEEKDAY(' + cellRef + ')=' + tableData[r][4][a].toString();
if (a == tableData[r][4].length - 1 && tableData[r][4].length > 1) {
formulaText += '),';
} else {
formulaText += ', ';
}
}
formulaText += cellRef + '>=' + tableData[r][2] + ', ' + cellRef + '<=' + tableData[r][3] + '), "' + tableData[r][0] + '", ';
rowCount = r+1;
}
formulaText += '"-"';
for (let p=0; p<rowCount; p++) {
formulaText += ')';
}
// create a new sheet
let newSheet = workbook.addWorksheet("My New Sheet");
// add the header row
let header = newSheet.getRange("A1:C1").setValues([["Activity", "Day", "Date"]])
// insert the oldest date into the first row, then add formula to adjacent cells in row
let firstDate = newSheet.getRange("C2")
firstDate.setValue(oldestDate);
firstDate.setNumberFormatLocal("m/d/yyyy");
firstDate.getOffsetRange(0, -1).setFormula("=C2");
firstDate.getOffsetRange(0, -1).setNumberFormatLocal("ddd");
firstDate.getOffsetRange(0, -2).setFormula(formulaText);
// use autofill to copy results down until the last day in the sequence
let autoFillRange = "A2:C" + (calendarSpread).toString();
firstDate.getResizedRange(0, -2).autoFill(autoFillRange, ExcelScript.AutoFillType.fillDefault);
// convert the the range to a table and format the columns
let outputTable = newSheet.addTable(newSheet.getUsedRange(), true);
outputTable.getRange().getFormat().autofitColumns();
//navigate to the new sheet
newSheet.activate();
}
// function to return days (as a number) for each day of week found in a string
function findDay(foo: string) {
// start with a list of days to search for
let daysOfWeek:string[] = ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"];
//create empty arrays
let searchResults:number[] = [];
let daysFound:number[] = [];
// search for each day of the week, this will create an array for each day of the week where the value is -1 if the day is not found or write the position where the day is found
for (let d of daysOfWeek) {
searchResults.push(foo.search(d));
}
// now take the search results array and if the number contained is greater than -1 add it's position+1 to a days found array. this should end up being a list of numbered days of the week found in a string/cell
for (let i = 0; i < searchResults.length; i++) {
if (searchResults[i] > -1) {
daysFound.push(i + 1);
}
}
return daysFound
}
I managed to get this working using below code for anyone who might be interested.
workbook.getWorksheet('UpdatedSheet')?.delete()
let usedRange = workbook.getActiveWorksheet().getTables()[0].getRangeBetweenHeaderAndTotal();
let newString: string[][] = [];
usedRange.getValues().forEach(row => {
let daysRows: string[][] = [];
let days = row[1].toString().split(',');
days.forEach(cellValue => {
if (cellValue != ' ') {
let eachDayData = row.toString().replace(row[1].toString(), cellValue).split(',');
daysRows.push(eachDayData);
}
});
daysRows.forEach(actualDay => {
const effDate = new Date(Math.round((actualDay[2] as unknown as number - 25569) * 86400 * 1000))
const disDate = new Date(Math.round((actualDay[3] as unknown as number - 25569) * 86400 * 1000))
getDatesInRange(effDate, disDate).forEach(element => {
let options = { weekday: 'short' }
if (element.toLocaleDateString('en-GB', options) == actualDay[1]) {
let datas = actualDay.toString().replace(actualDay[2], element.toDateString()).split(',')
datas.pop()
newString.push(datas)
}
});
});
});
workbook.getWorksheet('UpdatedSheet')?.delete()
let workSheet = workbook.addWorksheet('UpdatedSheet');
workSheet.activate();
let headers = workSheet.getRange('A1:C1').setValue([['Activity', 'Day', 'Date']])
let range = workSheet.getRange('A2');
let resizedRange = range.getAbsoluteResizedRange(newString.length, newString[0].length);
resizedRange.setValues(newString);
let tableRange = workSheet.getRange("A2").getSurroundingRegion().getAddress();
let newTable = workbook.addTable(workSheet.getRange(tableRange), true);
newTable.setName('updatedTable');
workSheet.getRange().getFormat().autofitColumns()
}
function getDatesInRange(startDate: { getTime: () => string | number | Date; }, endDate: string | number | Date) {
const date = new Date(startDate.getTime());
const dates: Date[] = [];
while (date <= endDate) {
dates.push(new Date(date));
date.setDate(date.getDate() + 1);
}
return dates;
}

Datagrid gives null values using for loop in WPF

I have a dataGrid with more than 100 rows in it. I am extracting it to an existing Excel file. I can open the file and add values to the sheet. My problem is, the value becomes null as soon as it gets to the 14th row.
I tried reversing the order of the data in the datagrid just to be sure that it's not the value or data in the dataGrid that is causing the issue but I still get the same result. Only the first 13 rows are extracted to the Excel sheet. The for loop still goes to the rest of the loop but it seems to not get the values.
Here is my code:
var path = #"D:\Reports\Sample.xlsx";
var excel = new Excel.Application {Visible = true};
var wb = excel.Workbooks.Open(path);
var ws = (Excel.Worksheet)wb.Sheets["summary"];
for (var i = 0; i < Grid.Columns.Count; i++)
{
for (int j = 0; j < Grid.Items.Count; j++)
{
var b = Grid.Columns[i].GetCellContent(Grid.Items[j]) as TextBlock; =====> ON THE 14th ROW, the "b" variable becomes null all throught out the for-loop
var myRange = (Range)ws.Cells[j + 2, i + 1];
try
{
if (b != null) myRange.Value2 = b.Text;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Thread.Sleep(100);
}
}
This is the Excel file created. Values are not extracted any more after the 14th row.

I want to write set of random numbers in an Excel sheet by iterating rows .I am able write random numbers and I am unable to store them in excel

I want to generate random numbers and parallel i want to write them in excel sheet in 1st column upto 15 rows. I tried with below script, but, it's not working.
I tried with below script, every time I want to store up to 15 numbers and those numbers should not be repeated.
I tried by using below methods, but I am not getting how can we iterate both of them. Random numbers and Excel cell.
public long Random_Number() {
Random rand = new Random();
long drand = (long)(rand.nextDouble()*1000000000L);
long correct = 0;
String numberString = Long.toString(drand);
if (numberString.length() == 8) {
System.out.println(drand+ "It's not a 9 digit");
}
else if (numberString.length() == 9) {
correct = drand;
System.out.println(correct);
}
return correct;
}
public void writeData_Int_SSN( int cellNum) {
try {
File src = new File("filename.xls");
Cell cell = null;
FileInputStream fis = new FileInputStream(src);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sh1 = wb.getSheetAt(0);
long gid = Random_Number();
String GroupID = Long.toString(gid);
System.out.println(GroupID);
int num = Integer.parseInt(GroupID);
for (int i = 1; i < 12; i++) {
System.out.println("Entering into excel sheet");
cell = sh1.getRow(i).getCell(cellNum);
System.out.println("Iterating cells");
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
System.out.println("We are entering numeric data");
int str1 = Integer.parseInt(cell.getStringCellValue());
System.out.println(str1);
cell.setCellValue(num);
}
}
FileOutputStream fout = new FileOutputStream(new File("filename.xls"));
wb.write(fout);
fout.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

Reading style names in table cell of doc file in Apache-POI

I am able to read the table cells, But I wanted also to read the applied style name of each cell of a row in a table. How can I achieve this?
EDIT
Following is the code snip which I have tried. By this I am able to read cell text also the applied pstyle(para style), but not able to read the rstyles.
private static void processDoc(String path) throws Exception {
POIFSFileSystem fis = new POIFSFileSystem(new FileInputStream(path));
HWPFDocument wdDoc = new HWPFDocument(fis);
// list all style names and indexes in stylesheet
/*for (int j = 0; j < wdDoc.getStyleSheet().numStyles(); j++) {
if (wdDoc.getStyleSheet().getStyleDescription(j) != null) {
System.out.println(j + ": " + wdDoc.getStyleSheet().getStyleDescription(j).getName());
} else {
// getStyleDescription returned null
System.out.println(j + ": " + null);
}
}*/
// set range for entire document
Range range = wdDoc.getRange();
for (int i = 0; i < range.numParagraphs(); i++) {
Paragraph p = range.getParagraph(i);
// check if style index is greater than total number of styles
if (wdDoc.getStyleSheet().numStyles() > p.getStyleIndex()) {
//System.out.println(wdDoc.getStyleSheet().numStyles() + " -> " + p.getStyleIndex());
StyleDescription style = wdDoc.getStyleSheet().getStyleDescription(p.getStyleIndex());
String styleName = style.getName();
// write style name and associated text
System.out.println(styleName + " -> " + p.text().replaceAll("[\u0000-\u001f]", ""));
} else {
System.out.println("\n" + wdDoc.getStyleSheet().numStyles() + " ----> " + p.getStyleIndex());
}
}
}

POI: Append rows to existing workbook

Using XSSFWorkbook, is it possible to append rows to an existing sheet? I am doing multiple writes (which was a PITA to solve due to a bug) and while I can write out new sheets multiple times, it does not appear that I can append.
What I am currently doing is the following:
Read sheet to my workbook.
Load workbook.
Append rows to workbook in memory
Write out again.
4 Does not appear to work, just ignores it completely!
I am aware that SXSSFWorkbook exists, but attempting to convert my existing XSSFWorkbook into a streaming workbook creates corruption upon write.
Is it possible to solve this concundrum?
Update: Changed code based on suggestion, but getting stream closed error.
Code: (The physical rows returns correctly, but nothing gets written out)
private void writeToSheetMultipleTimes(SXSSFWorkbook wb,
ReportTemplateStructure appA, File wbFile)
{
Sheet sheet = wb.getSheetAt(0);
log.info("Attempting multi-write to sheet: " + sheet.getSheetName());
for(int i = 0; i < 10; i++)
{
Row row = sheet.getRow(i);
if (row == null) {
row = sheet.createRow(i);
}
Cell cell = row.getCell(0, Row.CREATE_NULL_AS_BLANK);
cell.setCellValue("Written value:" + i);
int numRows = sheet.getPhysicalNumberOfRows();
log.info("Current row count: " + numRows);
try{
XSSFWorkbook xssfBook = (XSSFWorkbook)writeOutAndReadBack(wb);
wb.dispose();
wb = new SXSSFWorkbook(xssfBook);
} catch (Exception e)
{
log.error("Unable to perform multiple write to same sheet", e);
}
}
}
public Workbook writeOutAndReadBack(Workbook wb) {
if(!(wb instanceof SXSSFWorkbook)) {
throw new IllegalArgumentException("Expected an instance of SXSSFWorkbook");
}
Workbook result;
try {
FileOutputStream baos = new FileOutputStream(streamingWorkBookFile);
wb.write(baos);
InputStream is = new FileInputStream(streamingWorkBookFile);
result = new XSSFWorkbook(is);
} catch (IOException e) {
throw new RuntimeException(e);
}
return result;
}
You appear to be always making changes to Sheet 0, but you're calling createRow every time. This won't go well if there's already something there, eg on your second pass! You either need to add a new Sheet every time, or check if the Row is there with a call to getRow(int) first and only create if it is null.
If we look at your code snippet:
Sheet sheet = wb.getSheetAt(0);
for(int i = 0; i < 10; i++)
{
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
That should either be something like:
Sheet sheet = wb.createSheet();
for(int i = 0; i < 10; i++)
{
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
Or you should check first and only create missing rows/cells, eg
Sheet sheet = wb.getSheetAt(0);
for(int i = 0; i < 10; i++)
{
Row row = sheet.getRow(i);
if (row == null) {
row = sheet.createRow(i);
}
Cell cell = row.getCell(0, Row.CREATE_NULL_AS_BLANK);
You can use getPhysicalNumberOfRows() Method in XSSFSheet class to get last updated row number. Now create a new row by incrementing this value to append new data.

Resources