ÃֽŠ°Ô½Ã±Û(JAVA)
2018.07.30 / 10:29

ÀÚ¹Ù ¿¢¼¿ µ¥ÀÌÅÍ Àбâ : ÀÔ·ÂÇÑ Çü½Ä´ë·Î¸¸ Àоî¿À±â

summerman
Ãßõ ¼ö 244
¼­·Ð
¿¢¼¿ ¸®´õ¸¦ ÀÛ¼ºÇÒ ÀÏÀÌ ÀÖ¾î ¿Ã·Áº¾´Ï´Ù.


Å×½ºÆ® µ¥ÀÌÅÍ

C:/abc.xlsx
C:/abc.xls
°¢°¢ ÀúÀå


¾ÆÆÄÄ¡ poi-ooxml ¸¦ µðÆÒ´ø½Ã¿¡ µî·ÏÇÕ´Ï´Ù.
  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi-ooxml</artifactId>
  4. <version>3.15</version>
  5. </dependency>


ÀϹÝÀûÀÎ »ç¿ë
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8. import org.apache.poi.ss.usermodel.Row;
  9. import org.apache.poi.ss.usermodel.Sheet;
  10. import org.apache.poi.ss.usermodel.Workbook;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12. import org.junit.Test;
  13.  
  14. /**
  15. * ¿¢¼¿ jexcelapi test
  16. */
  17. public class ExcelReadTest
  18. {
  19. @Test
  20. public void excelRead() throws FileNotFoundException, IOException
  21. {
  22. List<String> paths = Arrays.asList("C:/abc.xlsx", "C:/abc.xls");
  23. for (String path : paths)
  24. {
  25. Workbook tempWorkbook;
  26. if (path.endsWith(".xls"))
  27. {
  28. tempWorkbook = new HSSFWorkbook(new FileInputStream(path));
  29. }
  30. else if (path.endsWith(".xlsx"))
  31. {
  32. tempWorkbook = new XSSFWorkbook(new FileInputStream(path));
  33. }
  34. else
  35. {
  36. throw new IllegalAccessError("xls / xlsx È®ÀåÀÚ¸¸ ÀÐÀ» ¼ö ÀÖ½À´Ï´Ù.");
  37. }
  38. try (Workbook workbook = tempWorkbook)
  39. {
  40. System.out.println("°æ·Î : " + path);
  41. Sheet sheet = workbook.getSheetAt(0);
  42. System.out.println("ù¹ø° sheet ÀÐÀ½");
  43. // ÇàÀ» °¡Á®¿É´Ï´Ù.
  44. for (Row row : sheet)
  45. {
  46. // cell (ÇàÀÇ °¢ ¿­) À» °¡Á®¿É´Ï´Ù.
  47. row.forEach(cell ->
  48. {
  49. System.out.print(cell.toString());
  50. System.out.print(" || ");
  51. });
  52. System.out.println();
  53. }
  54. }
  55. }
  56. }
  57. }
°á°ú :
°æ·Î : C:/abc.xlsx
ù¹ø° sheet ÀÐÀ½
±×³É¼ýÀÚ || ±×³É¹®ÀÚ || ¾Æ¹«°Å³ª || ³¯Â¥ ||
332492.0 || ¾È³çÇϼ¼¿ä || 3234.23 || 30-10¿ù-2011 ||
23422.0 || °¡¸®»ç´Ï || 3234.23 || 31-10¿ù-2011 ||
234234.0 || °³¹ßÀÚ || 3234.23 || 01-11¿ù-2011 ||
234234.0 || °ø°£ || 3234.23 || 02-11¿ù-2011 ||
234234.0 || ÀÔ´Ï´Ù. || 3234.23 || 03-11¿ù-2011 ||
234111.0 || Å×½ºÆ® || 3234.23 || 04-11¿ù-2011 ||
3679.0 || ¹®¼­ || 3234.23 || 05-11¿ù-2011 ||
2134234.0 || ÀÔ´Ï´Ù. || 3234.23 || 06-11¿ù-2011 ||
°æ·Î : C:/abc.xls
ù¹ø° sheet ÀÐÀ½
±×³É¼ýÀÚ || ±×³É¹®ÀÚ || ¾Æ¹«°Å³ª || ³¯Â¥ ||
332492.0 || ¾È³çÇϼ¼¿ä || 3234.23 || 30-10¿ù-2011 ||
23422.0 || °¡¸®»ç´Ï || 3234.23 || 31-10¿ù-2011 ||
234234.0 || °³¹ßÀÚ || 3234.23 || 01-11¿ù-2011 ||
234234.0 || °ø°£ || 3234.23 || 02-11¿ù-2011 ||
234234.0 || ÀÔ´Ï´Ù. || 3234.23 || 03-11¿ù-2011 ||
234111.0 || Å×½ºÆ® || 3234.23 || 04-11¿ù-2011 ||
3679.0 || ¹®¼­ || 3234.23 || 05-11¿ù-2011 ||
2134234.0 || ÀÔ´Ï´Ù. || 3234.23 || 06-11¿ù-2011 ||
¼ýÀÚ¿Í ³¯Â¥°¡.. ½É°¢ÇÕ´Ï´Ù....


ÀϹÝÀûÀÎ »ç¿ë
- À§ ¹æ¹ýÀº Æ÷¸äÀ» ¸¶À½´ë·Î ¹Ù²ã¹ö¸®´Â ´ÜÁ¡¶§¹®¿¡ DataFormatter ¸¦ ½áº¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8. import org.apache.poi.ss.usermodel.DataFormatter;
  9. import org.apache.poi.ss.usermodel.Row;
  10. import org.apache.poi.ss.usermodel.Sheet;
  11. import org.apache.poi.ss.usermodel.Workbook;
  12. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  13. import org.junit.Test;
  14.  
  15. /**
  16. * ¿¢¼¿ jexcelapi test
  17. */
  18. public class ExcelReadTest
  19. {
  20. @Test
  21. public void excelRead() throws FileNotFoundException, IOException
  22. {
  23. List<String> paths = Arrays.asList("C:/abc.xlsx", "C:/abc.xls");
  24. // µ¥ÀÌÅÍ Æ÷¸äÅÍ
  25. DataFormatter formatter = new DataFormatter();
  26. for (String path : paths)
  27. {
  28. Workbook tempWorkbook;
  29. if (path.endsWith(".xls"))
  30. {
  31. tempWorkbook = new HSSFWorkbook(new FileInputStream(path));
  32. }
  33. else if (path.endsWith(".xlsx"))
  34. {
  35. tempWorkbook = new XSSFWorkbook(new FileInputStream(path));
  36. }
  37. else
  38. {
  39. throw new IllegalAccessError("xls / xlsx È®ÀåÀÚ¸¸ ÀÐÀ» ¼ö ÀÖ½À´Ï´Ù.");
  40. }
  41. try (Workbook workbook = tempWorkbook)
  42. {
  43. System.out.println("°æ·Î : " + path);
  44. Sheet sheet = workbook.getSheetAt(0);
  45. System.out.println("ù¹ø° sheet ÀÐÀ½");
  46. // ÇàÀ» °¡Á®¿É´Ï´Ù.
  47. for (Row row : sheet)
  48. {
  49. // cell (ÇàÀÇ °¢ ¿­) À» °¡Á®¿É´Ï´Ù.
  50. row.forEach(cell ->
  51. {
  52. // µ¥ÀÌÅÍ Æ÷¸äÅÍ »ç¿ë.
  53. System.out.print(formatter.formatCellValue(cell));
  54. System.out.print(" || ");
  55. });
  56. System.out.println();
  57. }
  58. }
  59. }
  60. }
  61. }
°á°ú :
°æ·Î : C:/abc.xlsx
ù¹ø° sheet ÀÐÀ½
±×³É¼ýÀÚ || ±×³É¹®ÀÚ || ¾Æ¹«°Å³ª || ³¯Â¥ ||
332492 || ¾È³çÇϼ¼¿ä || 3234.23 || 10/30/11 ||
23422 || °¡¸®»ç´Ï || 3234.23 || 10/31/11 ||
234234 || °³¹ßÀÚ || 3234.23 || 11/1/11 ||
234234 || °ø°£ || 3234.23 || 11/2/11 ||
234234 || ÀÔ´Ï´Ù. || 3234.23 || 11/3/11 ||
234111 || Å×½ºÆ® || 3234.23 || 11/4/11 ||
3679 || ¹®¼­ || 3234.23 || 11/5/11 ||
2134234 || ÀÔ´Ï´Ù. || 3234.23 || 11/6/11 ||
°æ·Î : C:/abc.xls
ù¹ø° sheet ÀÐÀ½
±×³É¼ýÀÚ || ±×³É¹®ÀÚ || ¾Æ¹«°Å³ª || ³¯Â¥ ||
332492 || ¾È³çÇϼ¼¿ä || 3234.23 || 10/30/11 ||
23422 || °¡¸®»ç´Ï || 3234.23 || 10/31/11 ||
234234 || °³¹ßÀÚ || 3234.23 || 11/1/11 ||
234234 || °ø°£ || 3234.23 || 11/2/11 ||
234234 || ÀÔ´Ï´Ù. || 3234.23 || 11/3/11 ||
234111 || Å×½ºÆ® || 3234.23 || 11/4/11 ||
3679 || ¹®¼­ || 3234.23 || 11/5/11 ||
2134234 || ÀÔ´Ï´Ù. || 3234.23 || 11/6/11 ||
¼ýÀÚ´Â Á¦´ë·Î ³ª¿ÀÁö¸¸ ºÒÇàÀ̵µ ³¯Â¥°¡...
¾ÆÁÖ ºÒÇàÇÏ°Ôµµ ¿¹¿Ü ó¸® ¾øÀÌ Ã³¸®ÇÒ ¹æ¹ýÀÌ ¾ø´Â °Í °°½À´Ï´Ù.
xsl Áö¿øÀ» Æ÷±âÇÏ°í XSSFCell ·Î °¡´õ¶óµµ getRawValue °¡ ³¯Â¥Çü¿¡¼± ÀÔ·ÂÇÑ ´ë·Î ³ª¿ÀÁö ¾Ê½À´Ï´Ù.
±×·³ ¾î¶² Æ÷¸äÀ» ¿¹¿Üó¸®ÇØÁà¾ßÇÒ±î ¾Æ·¡ »çÀÌÆ®¿¡ µé¾î°¡ºÃ½À´Ï´Ù.
Âü°í : http://stackoverflow.com/questions/34918775/xlsx-issue-cell-getcellstyle-getdataformat-changing-value-after-adding-or-r
  1. int dataFormat = cell.getCellStyle().getDataFormat();
  2. if (dataFormat == 14) {
  3. dateFmt = "dd/mm/yyyy";
  4. } else if(dataFormat == 165) {
  5. dateFmt = "m/d/yy";
  6. } else if(dataFormat == 166) {
  7. dateFmt = "d-mmm-yy";
  8. } else if(dataFormat == 167) {
  9. dateFmt = "mmmm d yyyy ";
  10. } else if(dataFormat == 168) {
  11. dateFmt = "m/d/yyyy";
  12. } else if(dataFormat == 169) {
  13. dateFmt = "d-mmm-yyyy";
  14. }
¸Å¿ì ´ÙÇàÀ̵µ À§Ã³·³ ó¸®ÇÒ ÇÊ¿ä¾øÀÌ poi ÀÇ HSSFDateUtil ¿¡ ¾Æ·¡¿Í °°ÀÌ ÀÛ¼ºµÈ ºÎºÐÀÌ ÀÖ½À´Ï´Ù.
  1. /**
  2. * Given a format ID this will check whether the format represents
  3. * an internal excel date format or not.
  4. * @see #isADateFormat(int, java.lang.String)
  5. */
  6. public static boolean isInternalDateFormat(int format) {
  7. switch(format) {
  8. // Internal Date Formats as described on page 427 in
  9. // Microsoft Excel Dev's Kit...
  10. case 0x0e:
  11. case 0x0f:
  12. case 0x10:
  13. case 0x11:
  14. case 0x12:
  15. case 0x13:
  16. case 0x14:
  17. case 0x15:
  18. case 0x16:
  19. case 0x2d:
  20. case 0x2e:
  21. case 0x2f:
  22. return true;
  23. }
  24. return false;
  25. }
±×·³ Äڵ带 °íÃĺ¸°Ú½À´Ï´Ù.


³¯Â¥ ¿¹¿Ü ó¸®
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Arrays;
  6. import java.util.List;
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10. import org.apache.poi.ss.usermodel.DataFormatter;
  11. import org.apache.poi.ss.usermodel.Row;
  12. import org.apache.poi.ss.usermodel.Sheet;
  13. import org.apache.poi.ss.usermodel.Workbook;
  14. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  15. import org.junit.Test;
  16.  
  17. /**
  18. * ¿¢¼¿ jexcelapi test
  19. */
  20. public class ExcelReadTest
  21. {
  22. @Test
  23. public void excelRead() throws FileNotFoundException, IOException
  24. {
  25. List<String> paths = Arrays.asList("C:/abc.xlsx", "C:/abc.xls");
  26. // µ¥ÀÌÅÍ Æ÷¸äÅÍ
  27. DataFormatter formatter = new DataFormatter();
  28. // µ¥ÀÌÆ® Æ÷¸Ë
  29. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  30. for (String path : paths)
  31. {
  32. Workbook tempWorkbook;
  33. if (path.endsWith(".xls"))
  34. {
  35. tempWorkbook = new HSSFWorkbook(new FileInputStream(path));
  36. }
  37. else if (path.endsWith(".xlsx"))
  38. {
  39. tempWorkbook = new XSSFWorkbook(new FileInputStream(path));
  40. }
  41. else
  42. {
  43. throw new IllegalAccessError("xls / xlsx È®ÀåÀÚ¸¸ ÀÐÀ» ¼ö ÀÖ½À´Ï´Ù.");
  44. }
  45. try (Workbook workbook = tempWorkbook)
  46. {
  47. System.out.println("°æ·Î : " + path);
  48. Sheet sheet = workbook.getSheetAt(0);
  49. System.out.println("ù¹ø° sheet ÀÐÀ½");
  50. // ÇàÀ» °¡Á®¿É´Ï´Ù.
  51. for (Row row : sheet)
  52. {
  53. // cell (ÇàÀÇ °¢ ¿­) À» °¡Á®¿É´Ï´Ù.
  54. row.forEach(cell ->
  55. {
  56. String value;
  57. // ³¯Â¥Çü ¿¹¿Ü
  58. if (HSSFDateUtil.isInternalDateFormat(cell.getCellStyle().getDataFormat()))
  59. {
  60. value = sdf.format(cell.getDateCellValue());
  61. }
  62. // ±âŸ
  63. else
  64. {
  65. value = formatter.formatCellValue(cell);
  66. }
  67. System.out.print(value);
  68. System.out.print(" || ");
  69. });
  70. System.out.println();
  71. }
  72. }
  73. }
  74. }
  75. }
Ãâ·Â :
°æ·Î : C:/abc.xlsx
ù¹ø° sheet ÀÐÀ½
±×³É¼ýÀÚ || ±×³É¹®ÀÚ || ¾Æ¹«°Å³ª || ³¯Â¥ ||
332492 || ¾È³çÇϼ¼¿ä || 3234.23 || 2011-10-30 ||
23422 || °¡¸®»ç´Ï || 3234.23 || 2011-10-31 ||
234234 || °³¹ßÀÚ || 3234.23 || 2011-11-01 ||
234234 || °ø°£ || 3234.23 || 2011-11-02 ||
234234 || ÀÔ´Ï´Ù. || 3234.23 || 2011-11-03 ||
234111 || Å×½ºÆ® || 3234.23 || 2011-11-04 ||
3679 || ¹®¼­ || 3234.23 || 2011-11-05 ||
2134234 || ÀÔ´Ï´Ù. || 3234.23 || 2011-11-06 ||
°æ·Î : C:/abc.xls
ù¹ø° sheet ÀÐÀ½
±×³É¼ýÀÚ || ±×³É¹®ÀÚ || ¾Æ¹«°Å³ª || ³¯Â¥ ||
332492 || ¾È³çÇϼ¼¿ä || 3234.23 || 2011-10-30 ||
23422 || °¡¸®»ç´Ï || 3234.23 || 2011-10-31 ||
234234 || °³¹ßÀÚ || 3234.23 || 2011-11-01 ||
234234 || °ø°£ || 3234.23 || 2011-11-02 ||
234234 || ÀÔ´Ï´Ù. || 3234.23 || 2011-11-03 ||
234111 || Å×½ºÆ® || 3234.23 || 2011-11-04 ||
3679 || ¹®¼­ || 3234.23 || 2011-11-05 ||
2134234 || ÀÔ´Ï´Ù. || 3234.23 || 2011-11-06 ||


°á·Ð
ÂòÂòÇÑ ÆÁÀÌÁö¸¸... Á¶±ÝÀÌ¶óµµ µµ¿òÀÌ µÇ¼ÌÀ¸¸é ÇÕ´Ï´Ù.!!