いまだにでてくる文字コードがらみの問題のための 2015/01/18

いまだにでてくる文字コードがらみの問題

import java.io.UnsupportedEncodingException; public class TestMojicode { public static void main(String[] args) throws Exception { a(); } static void a() throws UnsupportedEncodingException { int[] cps = {0x2012,0x2013,0x2014,0x2015,0x2212}; for (int cp : cps) { System.out.println("============"); String hex = Integer.toHexString(cp); System.out.println("hex:" + hex); final String s = toS(cp); { int cp2 = cp(s.charAt(0)); System.out.println(s + ":" + cp2); } { String s1 = to(s, "MS932"); int cp2 = cp(s1.charAt(0)); System.out.println(s1 + ":" + cp2); } { String s2 = to(s, "SJIS"); int cp2 = cp(s2.charAt(0)); System.out.println(s2 + ":" + cp2); } } } static String to(String s, String charasetName) throws UnsupportedEncodingException { return new String(s.getBytes(charasetName), charasetName); // 一旦変換してもどす } static String toS(int cp) { char[] ch = Character.toChars(cp); return new String(ch); } static int cp(char c) { return Character.codePointAt(new char[] { c }, 0, 1); } }



import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import javax.imageio.stream.FileImageInputStream; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class TestPoiExcel extends TestMojicode { static final String[][] sss = { { "全角ハイフンマイナス", "-" }, { "半角ハイフンマイナス", "-" }, { "全角ハイフン", "‐" }, { "全角マイナス", "−" }, { "フィギュアダッシュ", "‒" }, { "全角ダッシュ", "—" }, { "二分ダッシュ", "–" }, { "ホリゾンタルバー", "―" }, { "全角長音", "ー" }, { "半角長音", "ー" }, { "罫線", "─" }, { "罫線", "━" }, { "いち", "一" }, }; public static void main(String[] args) throws Exception { c("/Users/nakawakashigeto/export/eclipse/workspace/workspace_closure/zzz_2015/src/doc-files/b.xls"); } static void c(String file) throws InvalidFormatException, IOException, URISyntaxException { File f = new File(file); InputStream inputStream = new FileInputStream(f); Workbook workbook = workbook(inputStream); Sheet sheet = workbook.getSheetAt(0); a(sheet); workbook.write(new FileOutputStream(f)); } static void a(Sheet sheet) throws InvalidFormatException, IOException, URISyntaxException { int pos = 0; for (String[] ss : sss) { set(sheet, pos, 0, ss[0]); set(sheet, pos, 1, ss[1]); set(sheet, pos, 2, to(ss[1], "MS932")); set(sheet, pos, 3, to(ss[1], "SJIS")); pos += 1; } } public static void set(Sheet sheet, int r, int c, String s) { Row row = sheet.getRow(r); if (row == null) { row = sheet.createRow(r); } Cell cell = row.getCell(c); if (cell == null) { cell = row.createCell(c); } cell.setCellValue(s); } public static Workbook workbook(InputStream inputStream) throws InvalidFormatException, IOException { return WorkbookFactory.create(inputStream); } }

: