• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Swapping between two columns values

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to ask how do we perform swapping between two columns values in excel using java. Please tell me. Thanks in advance.
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have tried anything so far?
you can make use of Apache POI--->this is a java API for Microsoft documents.
take a look at Quick Guide for better understanding of API.on the left side of the website,it has its index you can see plenty of options there,choose as you like.
You may have question in your mind how really will you implement this api in your code or how will you swap the value.take a piece of paper,try to build some logic,think about it-if you have to swap the value directly in excel,how would you have done.then try to correlate the previously done things with coding steps or as a pseudo code.
i hope my suggestion will help you in getting your solution!

Kind Regards,
Praveen.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used this code. I got swapping values but I want to put these values in excel sheet also. How can I do that. Please Help me. Thanks in advance.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gunjan.you have incorrectly used code tags.next time whenever you use it first thing is to do a selection of your code and then hit a code button.before hitting a submit button please review your post,it will make you know how exactly it looks after posting.

kind regards
Praveen.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Praveen, I got it. Next time I will try my best.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone please help me to solve the problem of this question. It's very urgent.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gunjan Jadon wrote:I used this code. I got swapping values but I want to put these values in excel sheet also. How can I do that. Please Help me. Thanks in advance.


read the "reading and writing" lesson in the quick guide link-i have provided it above.there you will learn how can you read and write to the excel file.read the lessons carefully accordingly with your requirement.everything is explained in it,if you stuck at some part don't shy to ask here.

Kind regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Their is a heading "Reading and Re-writing".try to understand how exactly they are reading and then rewriting the same excel file,it's not that complex,you can easily understand that.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Praveen, I want to ask you that when I print c1 then it returns c2 value and when I print c2 then it return c1 value. c1 and c2 are cells. I am unable to put these values in excel sheet.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Praveen, are you getting my point?
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i got your point,do you have read what i suggest in my previous post.show me your full code or what have you tried so far...
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
again you didn't use code tags-see your code should be b/w 2 square brackets-
["code=java] your code goes here [/code"]
i have used inverted commas for visibility of syntax of code tag,neglect it.

so you  are done with swapping,all you need now is to just write these values to excel sheet,is it?
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for writting do as below:


try this in your code and let me know when you are done.

Kind regards,
Praveen.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this but it gives an error.
java.lang.IllegalStateException: Cannot get a text value from a numeric cell
at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1015)
at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:399)
at org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:351)
at b2b_final.B2B_Final.cpu52_54(B2B_Final.java:359)
at b2b_final.B2B_Final.main(B2B_Final.java:912)

one column is system name and other one is date column.
In first row, values are system name and date which are string and after first row all values are numeric.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then use setCellType(CellType celltype) method of class-"org.apache.poi.hssf.usermodel.HSSFCell".
you can take a look at an example how this method is used here-setCellType-Example.
notice how they sets the celltype before writing in it.

Kind Regards,
Praveen.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I used this code but I got wrong output. I got 24 jan and 29 jan 1900 instead of 1-30 sept 2016 in date column.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see this
Capture.PNG
[Thumbnail for Capture.PNG]
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to swap these two column system name and date. when I used that code I got this type of output.
Capture1.PNG
[Thumbnail for Capture1.PNG]
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all take a look at this--->how to use code-tags.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

praveen kumaar wrote:First of all take a look at this--->how to use code-tags.


Is it correct format?
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gunjan Jadon wrote:Is it correct format?

Yes the format is correct though your code need some indentation and comments,it would make your code readable.comment will let us know what you are doing or like to do.keep these things in mind for the next time.

now come to your problem..
as a programmer,i have already mentioned,you need to have some strategies or kind of pseudo code before moving towards coding.you are getting these problems because:
--->you have not written your strategy i.e how really you are going to solve this problem.
--->you are not thinking about-how really would you solve this problem if asked to solve on excel directly.you may be thinking that it is really easy to do so on excel,perhaps it is,and even more easy through coding.
--->the most important things,you have not read the API carefully.for ex at line-21:

do you know what are you doing here,you are trying to set the cell type(type of the value in a cell) of given cell to string though it is containing a numerical value and consequently you are getting a exception.

so before moving towards coding be assured that you have done all these things.and believe me these things will let you enjoy your work even better.

like if i were at your place i would have written this before coding:-

as you are working on this project for a long time .i am providing you a very big hint.
Hint:

hope this time you will get a success!

Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will request some staff member to delete the duplicate post and  add code tags wherever necessary here,i have not keys for moderating the same.
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used this code. When I print cl1 cell then it prints correct output but in excel sheet it is returned incorrect output.


12.PNG
[Thumbnail for 12.PNG]
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am mentioning some part of your code.did you not think that the marked lines is kind of unnecessary thing in your code.


are you not comfortable with this approach..
 
Gunjan Jadon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

praveen kumaar wrote:i am mentioning some part of your code.did you not think that the marked lines is kind of unnecessary thing in your code.


are you not comfortable with this approach..



Hello Praveen, My problem is solved using your code. thank you so much.
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic