I am pretty new to Selenium and learning through self study. Any help will be appreciable.
Currently I am using
String data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getStringCellValue();
It is working fine for String but not for numeric. I have a cell value of '500' and I want to fetch it. Please help.
Try out following code :
public void readfile(String filepath, String filename, String sheetname) throws IOException {
File file = new File(filepath+"\\"+filename);
FileInputStream fis = new FileInputStream(file);
// for creating .xlsx workbook
Workbook wb = new XSSFWorkbook(fis);
// for reading the sheet by its name
Sheet sh = wb.getSheet(sheetname);
// find the total rows in sheet
int rowcount = sh.getLastRowNum() - sh.getFirstRowNum();
// create a loop to create
for (int i = 0; i < rowcount + 1; i++) {
Row row = sh.getRow(i);
// create a loop to print cell values
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(row.getCell(j).getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print((int)row.getCell(j).getNumericCellValue() + " ");
break;
}
}
System.out.println();
}
}
Hope it will help you.
Double data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getNumericCellValue();
Related
I have this code:
function _1() {
var aa = SpreadsheetApp.getActiveSpreadsheet();
var sheet = aa.getSheets()[0];
var cell = sheet.getRange("k1");
var cell = sheet.getvalue("a" + cell + ")");
};
In cell k1 there is a number (variable). I want to use this cell in my function, but its not work for me.
You entire code is correct, except last line:
var cell = sheet.getvalue("a" + cell + ")")
You don't need to import range value again in your getValue and it will cause error, and below is the correct execution by convert value to string (*remove ; after } ):
function test_1() {
var aa = SpreadsheetApp.getActiveSpreadsheet();
var sheet = aa.getSheets()[0];
var cell = sheet.getRange("B2").getValue();
cell = cell.toString() + "a"
Logger.log(cell);
}
Answer
getValue() does not take any input parameter. Join the strings later
Solution
I attach you a function that reads the number in B2 (X) and writes a string in AX.
function test_1() {
var aa = SpreadsheetApp.getActiveSpreadsheet();
var sheet = aa.getSheets()[0];
var B2 = sheet.getRange("B2").getValue();
var cell = "A" + B2.toString()
sheet.getRange(cell).setValue('test')
}
References
Range.getValue()
Range.setValue(value)
I know how to format cell values in EPPlus but only if the format applies to the whole value. How can I add formatting that only applies to part of the cell value?
for example...
Use the RichText collection to build it:
using (var pck = new ExcelPackage())
{
var wb = pck.Workbook;
var ws = wb.Worksheets.First();
var cell = ws.Cells["B2"];
cell.RichText.Add("This is some ");
cell.RichText.Add("formatting");
cell.RichText.Add(" withing a value");
cell.RichText[1].Color = Color.Red;
cell.RichText[1].Size = 14;
pck.Save();
}
XSSFWorkbook wbk = new XSSFWorkbook(file);
XSSFSheet sheets = wbk.getSheet("Sheet1");
Iterator row = sheets.iterator();
while (row.hasNext()){
Row rowItr = (Row) row.next();
Iterator cellItr = rowItr.cellIterator();
while (cellItr.hasNext()){
#SuppressWarnings("rawtypes")
Cell cell1 = (Cell) cellItr.next();
/* switch (cell1.getCellType()){
case Cell.CELL_TYPE_STRING:
data.add(cell1.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
data.add(cell1.getNumericCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
data.add(cell1.getBooleanCellValue());/*
In the commented line I am getting an error displaying
" cast The method getCellType() is undefined for the type Table.Cell "
. Why I am getting this error and how to fix it.
I need to loop through an array and populate a string in the following way
First --> string = array(0) + array(1) + array (2)
Then --> string = array(1) + array (2)
Then --> string = array(2)
How can I accomplish this if the length of the array is dynamic?
I am using vb.net
Thank you!
You can use 2 for loops to accomplish this. The first for loop would loop through the whole array and the send loop would be looping through the portion of the array that you want to add to the string.
var tempList = new ArrayList();
tempList.Add("Test1");
tempList.Add("Test2");
tempList.Add("Test3");
var tempString = new System.Text.StringBuilder();
for (int i = 0; i < tempList.Count; i++)
{
for (int j = i; j < tempList.Count; j++)
{
tempString.Append(tempList[j]);
}
}
i'm trying to make a program that inserts data into specific places in existing word document and saves a copy of it.
and i have no clue how to do it , and i cant find any good resource on office 2010 automating.
can anyone point me in the right direction and/or give me some examples.
thanks in advance.
found a solution will add an answer later on
Here is how i did it , it may not be the best way , but its working !
add references to the office interop
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
//defines new excel and workd apps
var ap = new Word.Application();
var excelApp = new Excel.Application();
// defines new excel worksheet & workbooks
Excel.Workbook exBook;
Excel.Worksheet xlWorkSheet;
// should the excell/word apps be visible ?
excelApp.Visible = false;
ap.Visible = false;
//defining the index numbers of our content controls that are in the word template
// index numbers start from 1 and are numbered by order of creation
object Price, Name, address;
Price = 1;
Name = 2;
address = 3;
// here we open the excell file
exBook = excelApp.Workbooks.Open(#"C:\test.xls");
// and we open the first worksheet
xlWorkSheet = exBook.Worksheets.get_Item(1);
Excel.Range range ;
//here we select the first worksheet and make it active
Excel._Worksheet workSheet = (Excel.Worksheet) excelApp.ActiveSheet;
//we open the word document
var doc = ap.Documents.Open(#"C:\test.dotx", ReadOnly: false, Visible: false);
// and we assign the content controls
var dPrice = doc.ContentControls.get_Item(ref Price);
var dName = doc.ContentControls.get_Item(ref Name);
var dAddress = doc.ContentControls.get_Item(ref address);
doc.Activate();
range = xlWorkSheet.UsedRange;
// here we define the columns that we are going to select
object t, P , E , K, N,M,J;
P = "P";
E = "E";
K = "K";
J = "J";
N = "N";
M = "M";
// and here we loop trought the rows of the excell worksheet
// IMPORTANT! excell rows count starts from 1 and not from 0 !
for (int i =1; i< Convert.ToInt16(Settings1.Default.copies) ;i++)
{
t = i;
// here we get the value if cell t(1..2..3..etc), P
var dummy = (range.Cells[t, P] as Excel.Range).Value2;
// here we insert the content of the cell to the content control
dPrice.Range.Text = ": " + Convert.ToString(dummy) + " лв";
dName.Range.Text = ": " + (string)(range.Cells[t, E] as Excel.Range).Value2;
// same thing here
var city = (string) (range.Cells[t, J] as Excel.Range).Value2;
var address1 = (string) (range.Cells[t, K] as Excel.Range).Value2;
var city2 = (string) (range.Cells[t, M] as Excel.Range).Value2;
var address2 = (string) (range.Cells[t,N] as Excel.Range).Value2;
if (!string.IsNullOrEmpty(city2) && city2 != " " && !string.IsNullOrEmpty(address2) && address2 != " ")
{
dAddress.Range.Text = ": " +city.Normalize() + " " + address1.Normalize() + " , " + city2.Normalize() + " " + address2.Normalize() ;
}
else
{
dAddress.Range.Text = ": " + city.Normalize() + " " + address1.Normalize();
}
try
{
//here we try to save the word document as a pdf file
object name = #"C:\t\test"+i+".pdf";
object FileFormat = WdSaveFormat.wdFormatPDF;
doc.SaveAs(ref name, ref FileFormat);
}
catch (Exception ex)
{
MessageBox.Show("Exception Caught: " + ex.Message +" source "+ ex.Source.ToString());
}
}
// here quit word without saving the changes to the template
ap.Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
excelApp.Quit();
// and we release the objects
System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
i hope this was helpful to someone :)