Help required in using same excel file as both Input and Output source

Hello Programmers, Here I am trying to read, modify and write an excel file using JAVA, I have successfully employed Jakarta POI and read the file but the problem is that I can''t make changes in the same file and save it.
I can't use the same file for "FileInputStream" and "FileOutputStream" I think this is causing major hurdle. Any kind of suggestion and help is truly appreciated and welcome. Thank you for your time. Here I am posting my code its a bit crude please pardon.
* @(#)attempt4.java
* attempt4
* @author
* @version 1.00 2008/3/26
import java.io.*;
import java.util.*;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hssf.usermodel.*;
public class attempt4 {
public static void main(String[] args) {
try{
File xlFile = new File("C:\\Drifter\\Study\\Krishnan\\Test.xls");
FileInputStream readFile = new FileInputStream(xlFile);
POIFSFileSystem fileSys = new POIFSFileSystem(readFile);
HSSFWorkbook xlSheetBook = new HSSFWorkbook(fileSys);
for(int i = 0; i < xlSheetBook.getNumberOfSheets(); i++) {
HSSFSheet sheetNow = xlSheetBook.getSheetAt(i);
System.out.println("\n Now Opening Sheet Number" + (i+1));
if(sheetNow.getPhysicalNumberOfRows() > 0) {
int numOfRows = sheetNow.getLastRowNum();
int numOfColumns = 0;
for(int cols = 0; cols < numOfRows; cols++) {
HSSFRow rowNow = sheetNow.getRow(cols);
if(rowNow != null) {
if(numOfColumns < sheetNow.getRow(cols).getLastCellNum()) {
numOfColumns = sheetNow.getRow(cols).getLastCellNum();
System.out.println("\n There are " + (numOfRows+1) + " Number of Rows");
System.out.println("\n There are " + numOfColumns + " Number of Columns");
for(int j = 0; j < numOfColumns; j++) {
float colTotal = 0;
int numOfNumericCells = 0;
for(int k = 0; k < numOfRows; k++) {
HSSFRow rowReading = sheetNow.getRow(k);
if(rowReading != null) {
HSSFCell cellReading = rowReading.getCell((short)j);
if(cellReading != null) {
if(cellReading.getCellType() == 0) {
colTotal += cellReading.getNumericCellValue();
numOfNumericCells ++;
else if(cellReading.getCellType() != 0) {
cellReading.setCellValue((double) 0.00);
System.out.println(" \nSum of Column " + (j+1) + " is " + colTotal);
System.out.println(" \nAverage of Column " + (j+1) + " is " + (colTotal/numOfNumericCells));
readFile.close();
catch(FileNotFoundException ex1) {
ex1.printStackTrace();
catch(IOException ex2) {
ex2.printStackTrace();
catch(NullPointerException ex3) {
ex3.printStackTrace();
The above code can successfully read the excel document and caliculates the sum and averages of the columns, can any one please help me in writing the same into the same file in additional rows. Thank you very much for your time.

May be you can try to create a temporary copy of the file, do your update on it and then overwrite the original.
Edited by: jgagarin on Jun 3, 2008 6:55 PM

Similar Messages

  • How can I use a different driver for audio input and output?

    I did a search of course, and came up with something about an aggregate. I have no idea what this is, how to do it, or if it would even work for me.
    What I am trying to do is:
    1) Record into Logic Express using my Tascam US-122.
    2) Have playback come out of my computer sound system, not the Tascam.
    If I go over to the Audio setup window, I can only record when the driver is set to Tascam US-122. Likewise, I can only listen to sound when my Built-In Audio is selected. It gets rather annoying going between the two.
    So, would this aggregate thing solve my problem? If so, how do I do it? Thanks for any help!
    -allen

    Yes it should do what you want.
    Go to "Audio Midi Setup", and go to the Audio menu and click "Open Aggregate Device Editor". The interface is pretty simple but if you do get stuck, just use the help function in Audio Midi Setup, as it has a step by step guide.
    Then when you return to logic, go to the Preferences>Audio>Drivers section and select Aggregate Device as the new driver rather than either the built in sound or the tascam. Then the inputs and outputs will apply to BOTH devices.

  • How to use a bluetooth cellphone for audio input and output

    Hello, I was wondering how to set-up my Mac Book so that I can use my bluetooth cellphone for audio in and audio out. Ideally, I would like to use my handset for talking on iChat and Skype. Has anyone had any luck doing this? When I bond my cellphone these optiosn do not appear. I know that this is easy to do on a Windows machine but I am not sure about how to do it on my Mac Book.
    Thanks!

    Yes it should do what you want.
    Go to "Audio Midi Setup", and go to the Audio menu and click "Open Aggregate Device Editor". The interface is pretty simple but if you do get stuck, just use the help function in Audio Midi Setup, as it has a step by step guide.
    Then when you return to logic, go to the Preferences>Audio>Drivers section and select Aggregate Device as the new driver rather than either the built in sound or the tascam. Then the inputs and outputs will apply to BOTH devices.

  • How can I use same bookmarks file for both Firefox & Internet Explorer?

    There are lots of questions and answers about how to import/export bookmarks/favorites to/from Internet Explorer, but they all seem to assume that afterward you're only going to use the one browser.
    But like many other people, although I prefer to use Firefox, there are times I need to use IE for certain sites, or to see how a page I'm creating will display to IE users. This means that I regularly edit bookmarks/favorites in both browsers, and can't possibly keep up with importing/exporting back and forth. What I need to do is use the same set of bookmarks with both browsers, so that the next time I bookmark a page in IE it will be available when I re-open Firefox and vice versa.
    I had things configured that way on my last computer, and it worked beautifully for years. Of course, it's long enough ago that I forgot how I did it, and a perfect memory wouldn't help anyway, since it was under Windows XP, and older versions of both browsers. Now I'm in the throes of learning Windows 7 on a new machine and haven't yet figured out how it thinks or wants me to think, so until I get my feet under me I'll need some pretty specific instructions. (Explanation would also be appreciated, as I do like to know the whys of things and it will help me become more proficient eventually; but at this moment my mental bandwidth is pretty low and my priority is just to get some basic things working.)

    Hi fastauntie,
    This add-on may be able to help: [https://addons.mozilla.org/en-US/firefox/addon/plainoldfavorites PlainOldFavorites]. It allows you to access your Internet Explorer favourites from within firefox. There are some instructions here: [http://www.howtogeek.com/59050/from-the-tips-box-simple-ie-to-firefox-syncing-easy-windows-toolbars-and-identifying-usb-cables/]
    Also you might be interested in [https://addons.mozilla.org/en-US/firefox/addon/ie-tab-2-ff-36 IE Tab v2]. It will allow you to use the Internet Explorer rendering engine from within firefox. It is useful if you want to see how a page would display in IE without leaving Firefox.
    I hope that helps,
    --Edward

  • Positional flat file schemas for input and output files to be generated with the required usecases

    Hello all,
    I need one help regarding the positional flat file schema which contains multiple headers, body and trailers.
    I have attached the sample input file below. This is a batched input and we have to generate the output which I have given below:
    We are unable to flat file schema which replicates the below input file. Please suggest better approach to achieve this.
    Sample Input FIle:
    010320140211ABC XYZ XYZ ABCD201402110 FSPAFSPA005560080441.02000006557.FSPA.IN AB-CD ABCD/AB-CD BETALKORT
    1020140210AN194107123459.FSPA.IN
    [email protected]
    1020140210AN196202123453.FSPA.IN
    [email protected]
    1020140210AN198103123435.FSPA.IN
    [email protected]
    1020140210AN195907123496.FSPA.IN
    [email protected]
    1020140210AN195903123437.FSPA.IN
    [email protected]
    1020140210AN193909123434.FSPA.IN
    [email protected]
    1020140210AN195607123413.FSPA.IN
    [email protected]
    1020140210AN199205123408.FSPA.IN
    [email protected]
    1020140210AN196206123499.FSPA.IN
    [email protected]
    1020140210AN196709123410.FSPA.IN
    [email protected]
    1020140210AN194708123455.FSPA.IN
    [email protected]
    1020140210AN195710123443.FSPA.IN
    [email protected]
    1020140210AN198311123436.FSPA.IN
    [email protected]
    1020140210AN196712123471.FSPA.IN
    [email protected]
    1020140210AV197005123403.FSPA.IN
    98000000000000014000000000000001000000000000015
    010320140211ABC XYZ XYZ PEDB201402111 FSPAICA 005560080441.02000006557.FSPA.IN AB-CDABCD/ABCDBETALKORT
    1020140210AN195111123491.ICA.IN
    [email protected]
    1020140210AV195309123434.ICA.IN
    98000000000000001000000000000001000000000000002
    Output FIle:
    1020140210AN195607123413.FSPA.IN
    [email protected]
    1020140210AN199205123408.FSPA.IN
    [email protected]
    1020140210AN196206123499.FSPA.IN
    [email protected]
    1020140210AN196709123410.FSPA.IN
    [email protected]
    1020140210AN194708123455.FSPA.IN
    [email protected]
    1020140210AN195710123443.FSPA.IN
    [email protected]
    1020140210AN198311123436.FSPA.IN
    [email protected]
    1020140210AN196712123471.FSPA.IN
    [email protected]
    1020140210AN195111123491.ICA.IN
    110019EPS [email protected]
    1020140210AV197005123403.FSPA.IN
    1020140210AV195309123434.ICA.IN
    98000000000000001000000000000001000000000000002
    99000000000000001
    Note: Header is a single line till BETALKORT and also there is a space before email id. That is not getting pasted properly in the files.
    Thanks and Regards,
    Veena Handadi

    Hi all,
    Header is missed from the output file for the above post:
    Please find the output file:
    010320140211ABC XYZ XYZ ABCD201402110 FSPAFSPA005560080441.02000006557.FSPA.IN AB-CD ABCD/AB-CD BETALKORT
    1020140210AN195607123413.FSPA.IN
    [email protected]
    1020140210AN199205123408.FSPA.IN
    [email protected]
    1020140210AN196206123499.FSPA.IN
    [email protected]
    1020140210AN196709123410.FSPA.IN
    [email protected]
    1020140210AN194708123455.FSPA.IN
    [email protected]
    1020140210AN195710123443.FSPA.IN
    [email protected]
    1020140210AN198311123436.FSPA.IN
    [email protected]
    1020140210AN196712123471.FSPA.IN
    [email protected]
    1020140210AN195111123491.ICA.IN
    110019EPS [email protected]
    1020140210AV197005123403.FSPA.IN
    1020140210AV195309123434.ICA.IN
    98000000000000001000000000000001000000000000002
    99000000000000001

  • How can I substitute characters in a string when the same characters are used in both input and output strings

    Hi Numbers users,
    I am trying to get Numbers 2.3 on OS X (10.9.1) to convert  cells containing variable length strings composed of 6 letters, A, B, C, a, b, c into a second string containing the same 6 lettersusing the rule A to c, B to b, and C to a and vice versa, so for example cell A1 might contain string "AcBbCaaB" and I want Numbers to convert this into "cAbBaCCb" in cell B1. I have tried to use the SUBSTITUTE function (nested) without success so far as I end up with the letters either all uppercase or all lowercase. I understand why this is happening and maybe it isn't possible using SUBSTITUTE. Does anyone know if there is another function I can use to do this?
    Thanks, Hugh.

    you can use this method:
    Add a new table, called "Translator", show as the bottom table:
    Enter the data in columns B anc C as shown
    cell A1 contains:
    "=CODE(B1)"
    select cell A1 and fill down
    For the table on top enter your code in column A.  The converted code will be in column B.  Make sure there are enough columns (I went out to column AD):
    B2=R2&S2&T2&U2&V2&W2&X2&Y2&Z2&AA2&AB2&AC2&AD2
    C2=LEN(A2)
    D2=MID($A2, COLUMN()−2, 1)
    select D2, copy,
    select cells D2 thru P2, paste
    R2=IFERROR(VLOOKUP(CODE(D2), Translator::$A:$C, 3, 0), "")
    select R2, copy,
    select cells R2 thru AD2, paste
    select B2 thru AD2 and fill down as needed
    You can hide columns C thru AD

  • Can i use same iPod classic with both windows and apple machines

    can i download audible books, music and podcasts from windows and apple machines on the same ipod classic?
    thank you    joanlvh

    Yes you can. You will have to authorize both machines for your iTunes account, and you will only be able to sync what is on the individual computers to your iPod, but switching between Mac and PC isn't an issue.

  • Are operating files only within Windows (C:) or are Word and Excel files ok in Documents and (C:) folders in 8.1?

    A Microsoft tech said "oh-oh" when he saw Word and Excel files, and I think other files, in Windows (C:). He said only operating files should be in that folder or a user might delete an operating file by mistake. I have Word and Excel files in
    both (C:) and Documents folders. There is some overlap but there are different files in each. When I open a Word file it defaults to (C:) not Documents. Sometimes default location is SkyDrive. So unpredictability is my experience. The questions are what is
    correct default and how to make it so.
    Microsoft store in San Diego transferred files from old computer to new one and this default is how the computer was set up.

    Hi, Tyro.
    What a mess. Usually system files in the Windows folder are protected from being deleted by users, but not all of them.
    Here are some ideas:
    Go back to the Microsoft store and tell them to fix it. They caused it, it was their mistake, they are responsible to fix it.
    If that is not possible because they are too far away... find another Microsoft Store if possible and have them fix it.
    Last resort: If it was me, I'd just back up all the files that I care about onto my SkyDrive (now OneDrive) account or a reliable thumb drive. Then do a factory re-image. Maybe back up the files and make the MS store do it. If I had purchased Office
    2013 (it sounds like you have Office 2013), then I would have them reinstall that as well.
    Keep the store involved because it is their fault.
    At least call the MS store in San Diego and ask them what they suggest. Put them on the spot. They are the ones that screwed up and are responsible for the copying process.
    Good luck!
    Best wishes, Davin Mickelson

  • Audio input and output at the same time

    Hello mac users.
    I have one simple question: since the new macbook has one single audio port for both input and output, how could I, for example, play my guitar AND listen to the headphones at the same time?
    I KNOW (because I've tried) that with the iPhone headphones with microphone the port CAN physically work at the same time for input and output, so the question is: what can I use to do that? Will a simple splitter do or do I have to use different tricks?
    (I know there is a thread on this already, but it doesn't provide a definitive answer)
    Thank you!

    One of the first questions to ask...
    Is the audio input stereo or mono?
    When using a headset mic you're dealing with a mono input so you at least know the Mac is capable of that.
    If it's mono, and I suspect it is, should be easy to either make or purchase a custom cable that has two female input connectors, one mono input and a stereo headphone input.
    However, I'd probably just purchase a USB audio interface, the sound, both recording and playback will be head and shoulders above the Macbooks built in internal audio.
    pancenter-

  • Using Labview how can one store different data in different sheets of same excel file, I mean how to select different sheets to store data??

    Hello Everyone,
    I want to store various data but in different sheets of excel file. So how to select Different sheets of same excel file???
    Thanks so much 
    Pallavi 

    aeastet wrote:
    Why do you not want to use Active X?
    One very good reason that I can think of is that MS keeps changing their ActiveX interface with each version of Excel, so code written for one version of Excel using ActiveX may not work for another version of Excel, even though the basic functionality hasn't changed. A perfect example is when MS changed the "Value" property to "Value2". Yeah, that made a whole lot of sense.
    pals wrote:
    I dont want to use active X as i am not
    getting results... by using write to spreadsheet in am getting results
    but on just one sheet... I want different data on different sheets of
    same excel file. So....
    Can anyone help me in this...
    Then it's something you're doing. Please post your code. Have you tried a search on this forum for ActiveX and Excel? There have been tons of posts on it, including lots of examples. There's also the Excel thread.

  • How to use a excel file to create a service component?

    Dear all,
       I want to use a excel file to create a service to be my data source. I used the wizard to do that but i always got a message. "The data format is not valid, fields in each record must be delimited by tab charactors, and each record seperated by new line charactors". How should I reformat my excel file to fit the requirements? I have already chang the field`s format into 'text' type. Thx ^^

    Hi Louis,
    [Here|http://img125.imageshack.us/my.php?image=exceltestlo7.jpg] is the result of your data.  I downloaded your excel sheet and created a simulated service in 7.1.1 and it works fine.
    Below are the steps:
    1. Go to Tools -> service component wizard
    2. The popup opens : Step1 :  Enter the name of your service component
    3. Go to Excel spreadsheet Copy the data for which the service needs to be created.
    4. Step 2 : Paste the copied Excel data in the popup
    5. Step 3 : Define Input -> just click next
    6. Step 4: Define Conditions -> just click finish.
    Let me know if it helps.
    Good Luck,
    Dharmi

  • Using an Excel file as a dynamic data source? Opinions needed...

    I have posted this topic before, but as always; in order to get the relevant correct answer you have to ask the correct question.
    I'm trying to create a number of Pricelists linked to an Excel/CSV file. I have a Excel file that contains Pricelist information which is Product specific.
    I have had a number of suggestion that follow:
    A direct link to the Excel file. PROs: Excel file can be uploaded on FTP and overwritten if (and when) amended. Linking this is easy peasy in Dreamweaver. Person browsing can download info straight away on request - no hassle. CONs: Simply, not everyone has Excel and those who don't can not access the information.
    Import Excel file as tabular data. PROs: Fairly easy to do in Dreamweaver. Person browsing can see info straight away. CONs: Can be time consuming on larger Excel files. NOT amendable (so a number of price changes becomes a big job). Can't simply overwrite Excel file on FTP. Larger Excel files can take a lot of page space and thus require tonnes of scrolling).
    Use the Excel file as a dynamic data source. --Not entirely sure how I would go about doing this (any suggestions/links/tutorials etc)-- PROs: ? CONs: Contributor added this suggestion is not a good idea as it performs poorly on the web.
    Create a dynamic page using a database and import the Excel file to that....or maintain the price list in the database rather than an Excel file. --Again not entirely sure how I would go about doing this (any suggestions/links/tutorials etc)-- My understanding of this option is that it will require XML data and SPRY work. Is this correct? Can this be someone who is not an advanced user?
    If once again, I'm off the mark and better suggestions can be thought - please do so.
    As you can see I'm at a bit of a crossroads so an suggestions, comment, help, links, tutorials or applause would be greatly received.
    Thanks in advance and looking forward to seeing the comments this throws up!

    Hi
    Although not everyone has excel just about everyone can open csv files in some way, if not offer the option to download "open office" which is free for the pc, as for the mac they have a program installed by default to use csv files.
    The import tabular data is not really an option for the reasons stated.
    Use excel as data source - not a good idea, requires asp.net to work correctly otherwise it does run slow and is not recommended if you are expecting more than a very small number of users.
    As for the dynamic with database, this can be done with xml and spry but if you have a large amount of data this is almost as bad as the option above. You are probably better creating a database and importing your excel spreadsheet into this, for tutorials on creating a php/mysql database and set-up of testing server see - http://www.adobe.com/devnet/dreamweaver/application_development.html.
    No matter which way you go with the last option it will require a fair amount of knowledge and experience to do correctly, efficiently and securely.
    PZ
    www.pziecina.com

  • Using Same sql file in different database

    Hi ,
    I need to use same sql file which contains table ddl for sybase as well as oracle .
    here If the db is sybase should run sybase ddl only and visa-ver.
    Please help
    Regards

    Its already there, but you have any though on same file please write.My thoughts is that it sounds like a bad idea. Do you have a particular reason for wanting this in a single file?

  • Excel file(.xls) as attachment and Body(Signature) via Receiver Mail Adapter

    Hi PI Experts,
    I have a requirement File(.txt) - PI(7.4) - mail (.xls attachment and signature in the body). Can anyone pls help me with the best approach to send excel file as mail attachment and signature in the body.
    Thanks,
    Sri

    Hi
    You can try this approach
    1. use mail package and java mapping to populate the mail package.
    inside the java mapping do the following
    a. read the text file and convert it to excel and then set it as additional attachment.
    b. map the signature and body with the field 'content' of mail package.
    2. on the receiver mail adapter check both mail package and keep attachment options.
    Hopefully this will work.
    Reference links
    excel write : Excel and Java - Read and Write Excel with Java - Tutorial
    set attachment: How to save MainDocument as additonal attachment in sender channel?

  • Main.vi creates 2D Arrays and I want to save the arrays in an microsoft Excel file(e.g engine_1)and in differnt worksheets(e.g. measure_1;measure_2;...)

    main.vi creates 2D Arrays and I want to save the arrays in an microsoft Excel file(e.g engine_1)and in differnt worksheets(e.g. measure_1;measure_2;...)
    The path(Grundpfad)for the excel file should be inserted in the main.vi.

    1. There's a shipping example called Write Table To XL.vi (Help>Find Examples>Communicating with External Applications>ActiveX>Excel) that you can use as a starting point.
    2. Buy the LabVIEW Report Generation Toolkit. Info can be found here.
    3. Search this forum for other examples of writing to Excel.

Maybe you are looking for