Java는 데이터를 SqlServer로 가져오기 위해 Excel을 처리합니다.

내 요구 사항은 다음과 같습니다.

사용자 가져오기 = "데이터베이스의 일부 사용자는 가져올 필요가 없습니다:: 계정 없이 가져온 이름에 해당하는 병음
여기에 이미지 설명 삽입

솔루션 단계

Excel에서 null 값을 해결하는 방법

Excel에서 null 값을 찾습니다.

Excel 중국어 변환 병음

Excel 중국어를 병음으로

자바 중국어 변환 병음

pinyin4j.jar 패키지를 가져와야 합니다.
여기에 이미지 설명 삽입

package www.yzq.com.tool;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class HanToPin {
    
    

	    /**
	     * 测试main方法
	     * @param args
	     */
	    public static void main(String[] args) {
    
    
	        System.out.println(ToFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写
	        System.out.println(ToPinyin("汉字转换为拼音")); 
	    }
	    /**
	     * 获取字符串拼音的第一个字母
	     * @param chinese
	     * @return
	     */
	    public static String ToFirstChar(String chinese){
    
             
	        String pinyinStr = "";  
	        char[] newChar = chinese.toCharArray();  //转为单个字符
	        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 
	        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
	        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
	        for (int i = 0; i < newChar.length; i++) {
    
      
	            if (newChar[i] > 128) {
    
      
	                try {
    
      
	                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);  
	                } catch (BadHanyuPinyinOutputFormatCombination e) {
    
      
	                    e.printStackTrace();  
	                }  
	            }else{
    
      
	                pinyinStr += newChar[i];  
	            }  
	        }  
	        return pinyinStr;  
	    }  
	   
	    /**
	     * 汉字转为拼音
	     * @param chinese
	     * @return
	     */
	    public static String ToPinyin(String chinese){
    
              
	        String pinyinStr = "";  
	        char[] newChar = chinese.toCharArray();  
	        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();  
	        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
	        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
	        for (int i = 0; i < newChar.length; i++) {
    
      
	            if (newChar[i] > 128) {
    
      
	                try {
    
      
	                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];  
	                } catch (BadHanyuPinyinOutputFormatCombination e) {
    
      
	                    e.printStackTrace();  
	                }  
	            }else{
    
      
	                pinyinStr += newChar[i];  
	            }  
	        }  
	        return pinyinStr;  
	    }  
	    
}

자바 연결 엑셀 출력 데이터

jxl.jar 패키지를 가져와야 합니다.
여기에 이미지 설명 삽입

package www.yzq.com.tool;

import java.io.File;

import jxl.Sheet;
import jxl.Workbook;

public class ExcelImport {
    
    
	public static void main(String[] args) {
    
    
		ExcelImport excelImport = new ExcelImport();
		excelImport.getAllByExcel("c://dfs.xls");
	}

	/**
	 * 查询指定目录中电子表格中所有的数据
	 * 
	 * @param file
	 *            文件完整路径
	 * @return
	 */
	public static void getAllByExcel(String file) {
    
    
		try {
    
    
			Workbook rwb = Workbook.getWorkbook(new File(file));
			Sheet rs = rwb.getSheet(0);// 或者rwb.getSheet(0)
			int clos = rs.getColumns();// 得到所有的列
			int rows = rs.getRows();// 得到所有的行

			for (int i = 1; i < rows; i++) {
    
    
				for (int j = 0; j < clos; j++) {
    
    
					// 第一个是列数,第二个是行数
					String id = rs.getCell(j++, i).getContents();// 默认最左边编号也算一列 所以这里得j++
					String name = rs.getCell(j++, i).getContents();
					String sex = rs.getCell(j++, i).getContents();
					String num = rs.getCell(j, i).getContents();
					
					System.out.println("id:" + id + " name:" + name + " sex:" + sex + " num:" + num);
				}
			}
		} catch (Exception e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

자바 연결 데이터베이스

jar 패키지를 가져와야 합니다.
여기에 이미지 설명 삽입

package www.yzq.com.tool;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class LinkSqlserver {
    
    
	    public static void main(String[] args) {
    
    
	        String user = "cczu";
	        String password = "****";
	        Connection conn;
	        Statement stmt;
	        ResultSet rs;
	        String url = "jdbc:sqlserver://192.168.1.99:1433;DatabaseName=QY_NTXC;";
	        String sql = "select * from t_user";
	        try {
    
    
	            // 连接数据库
	            conn = DriverManager.getConnection(url, user, password);
	            // 建立Statement对象
	            stmt = conn.createStatement();
	            // 执行数据库查询语句
	            rs = stmt.executeQuery(sql);
	            while (rs.next()) {
    
    
	                String id = rs.getString("LOGIN_NAME");
	                String name = rs.getString("NAME");
	                String score = rs.getString("EMAIL");
	                String sex = rs.getString("PHONE");
	                System.out.println("登录名: "+id+"昵称"+name+"邮箱 "+score+"电话"+sex);
	            }
	            if (rs != null) {
    
    
	                rs.close();
	                rs = null;
	            }
	            if (stmt != null) {
    
    
	                stmt.close();
	                stmt = null;
	            }
	            if (conn != null) {
    
    
	                conn.close();
	                conn = null;
	            }
	        } catch (SQLException e) {
    
    
	            e.printStackTrace();
	            System.out.println("数据库连接失败");
	        }
	    }
}

관계 구축

위의 방법을 연결하여 연결을 완료하십시오.

추천

출처blog.csdn.net/weixin_44077556/article/details/109240385