Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: how to read chinese data from excel to java

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to read chinese data from excel to java

    while iam reading data from excel to java, iam getting question marks.
    iam using workbook
    code is as follows

    while (rows.hasNext()) {
     
    				boolean isRecordExist = false;
    				HSSFRow row = (HSSFRow) rows.next();
     
    				if (row.getRowNum() == 0) {
    					continue;
    				}
     
    				String key = row.getCell((short) 0).getStringCellValue();
    				if(key.trim().length() == 0){
    					continue;
    				}
     
    				String value = row.getCell((short) 1).getStringCellValue();


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: how to read chinese data from excel to java

    Hello swapna.

    Welcome to the Java Programming Forums.

    Can you please post all of your code and also attach an example spread sheet with the Chinese writing?
    I would like to test this.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to read chinese data from excel to java

    package com.i18n.action;
     
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.upload.FormFile;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
     
    import com.i18n.exception.FileUploadException;
    import com.i18n.formbean.FileUploadForm;
    import com.i18n.persistance.Label;
    import com.i18n.util.HibernatePlugIn;
     
    public class FileUploadAction extends Action {
     
    	public ActionForward execute(ActionMapping mapping, ActionForm form,
    			HttpServletRequest request, HttpServletResponse response)
    			throws Exception {
     
    		FileUploadForm uploadForm = (FileUploadForm) form;
    		FileOutputStream outputStream = null;
    		FormFile formFile = null;
    		try {
     
    			formFile = uploadForm.getTheFile();
    			saveRecords(uploadForm,request);
     
    		} catch (Exception e) {
    			throw new FileUploadException();
    		}
     
    		finally {
    			if (outputStream != null) {
    				outputStream.close();
    			}
    		}
    		request.setAttribute("tabindex", "2");
    		return mapping.findForward("index");
    	}
     
    	private void saveRecords(FileUploadForm uploadForm, HttpServletRequest request) throws Exception {
     
    		// Create an excel workbook from the file system.
    		//
    		FormFile formFile = uploadForm.getTheFile();
     
     
    		HSSFWorkbook workbook = new HSSFWorkbook(formFile.getInputStream());
    		//
    		// Get the first sheet on the workbook.
    		//
    		HSSFSheet sheet = workbook.getSheetAt(0);
    		//
    		// When we have a sheet object in hand we can iterator on
    		// each sheet's rows and on each row's cells. We store the
    		// data read on an ArrayList so that we can printed the
    		// content of the excel to the console.
    		//
     
    		Iterator rows = sheet.rowIterator();
    		SessionFactory factory = null;
    		Session session = null;
    		Label label = null;
    		List duplicateLabelList = new ArrayList();
    		Map newLabelMap = new HashMap();
    		Set s = new HashSet();
    		try {
     
    			while (rows.hasNext()) {
     
    				boolean isRecordExist = false;
    				HSSFRow row = (HSSFRow) rows.next();
     
    				if (row.getRowNum() == 0) {
    					continue;
    				}
     
    				String key = row.getCell((short) 0).getStringCellValue();
    				if(key.trim().length() == 0){
    					continue;
    				}
     
    				String value = row.getCell((short) 1).getStringCellValue();
     
     
    				// create persistance object to save data
     
    				/*
    				 * query to find label is exist if exist update the label else
    				 * create new label and save the record
    				 */
    				// get the sesionfactory from servlet context
    				factory = (SessionFactory) servlet.getServletContext().getAttribute(
    						HibernatePlugIn.SESSION_FACTORY_KEY);
    				// create session object
    				session = factory.openSession();
     
    				// create the transaction object
    				Transaction tx = session.beginTransaction();
    				Query query = session
    						.createQuery("from Label label where label.msg_Key =:param1");
    				query.setString("param1", key);
    				List labelList = query.list();
     
    				// check whether the record exist
     
    				if (labelList.size() != 0) {
    					// if exist get the record and set the new value for update
    					label = (Label) labelList.get(0);
    					label.setModified_Date(new Date());
    					isRecordExist = true;
     
     
    				} else {
    					// if not exist create the new record
    					label = new Label();
    					label.setMsg_Key(key);
    					label.setCreated_Date(new Date());
    				}
     
    				// get the languget from form object
    				int lang_val = Integer.parseInt(uploadForm.getLanguage());
     
    				/*
    				 * select the language to which the label should add
    				 */
     
    				switch (lang_val) {
     
    				case 1:
    					label.setEnglish_Msg(value);
    					break;
     
    				case 2:
    					label.setFrench_Msg(value);
    					break;
     
    				case 3:
    					label.setGerman_Msg(value);
    					break;
    				case 4:
    					label.setDutch_Msg(value);
    					break;
    				case 5:
    					label.setSpanish_Msg(value);
    					break;
    				case 6:
    					label.setItaly_Msg(value);
    					break;
    				case 7:
    					label.setJapan_Msg(value);
    					break;
    				case 8:
    					label.setKorea_Msg(value);
    					break;
    				case 9:
    					label.setPortuguese_Msg(value);
    					break;
    				case 10:
    					label.setChinese_Msg(value);
    					break;
    				case 11:
    					label.setChineseTraditional_Msg(value);
    					break;
    				}
     
    				// check if record exist
    				if (isRecordExist) {
    					// if yes update the record
    					session.update(label);
    					System.out.println("updating record is " + label.getMsg_Key());
     
     
    				} else {
    					// else save the new record
    					//session.save(label);
    					newLabelMap.put(label.getMsg_Key(), label);
    					System.out.println("new record is " + label.getMsg_Key());
    				}
     
    				tx.commit();
     
    				if(!s.contains(key)){
    					s.add(key);
    				}else{
    					duplicateLabelList.add(label);
    				}
     
    			}// end of rows iteration
    			request.setAttribute("DuplicateLabelList", duplicateLabelList);
    			request.setAttribute("NewLabelList", newLabelMap.values());
    			request.getSession().setAttribute("NewLabelsMap", newLabelMap);
     
    		} catch (Exception e) {
     
    			e.printStackTrace();
     
    			throw e;
     
    		} finally {
     
    			session.close();
     
    		}
    	}
     
    }

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to read chinese data from excel to java

    copy & paste following chinese characters in the excel sheet
    订单中心
    用户名:
    密码:
    我的销售编号
    所有销售编号
    选择销售组织并提供搜索标准
    选择销售组织
    销售对象编号:
    销售对象名称:
    销售对象所在城市:
    搜索

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to read chinese data from excel to java

    swapna, did you ever get this figured out?

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: how to read chinese data from excel to java

    Well, this topic doesn't relate in this section, instead move it to the J2EE section.

    And if you are finding ? instead of characters, it clearly means that, it is not getting the correct unicodes from the excel sheet. Well, i will recommend you to try making an ArrayList of Chinese Characters UniCodes, get a character from excel sheet, compare it with the unicodes in the arraylist and map them to your output.

    I have tried this an year ago. Problem was somehow different but related to localization.

Similar Threads

  1. Read stream of data
    By mapred in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 26th, 2012, 12:38 PM
  2. Importing excel data
    By supriya ramjee in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 20th, 2012, 09:39 AM
  3. Replies: 1
    Last Post: April 7th, 2011, 07:01 AM
  4. Read excel file
    By chamil in forum JDBC & Databases
    Replies: 1
    Last Post: October 25th, 2010, 03:00 AM
  5. How to fetch integer data from excel
    By nehakuls in forum JDBC & Databases
    Replies: 5
    Last Post: May 14th, 2010, 01:44 AM