ÃֽŠ°Ô½Ã±Û(JAVA)
2018.07.30 / 10:29
ÀÚ¹Ù ¿¢¼¿ µ¥ÀÌÅÍ Àбâ : ÀÔ·ÂÇÑ Çü½Ä´ë·Î¸¸ Àоî¿À±â
summerman
Ãßõ ¼ö 244
¼·Ð
¿¢¼¿ ¸®´õ¸¦ ÀÛ¼ºÇÒ ÀÏÀÌ ÀÖ¾î ¿Ã·Áº¾´Ï´Ù.
Å×½ºÆ® µ¥ÀÌÅÍ
C:/abc.xlsx
C:/abc.xls
°¢°¢ ÀúÀå
¾ÆÆÄÄ¡ poi-ooxml ¸¦ µðÆÒ´ø½Ã¿¡ µî·ÏÇÕ´Ï´Ù.
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml</artifactId>
- <version>3.15</version>
- </dependency>
ÀϹÝÀûÀÎ »ç¿ë°á°ú :
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.List;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- 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.xssf.usermodel.XSSFWorkbook;
- import org.junit.Test;
- /**
- * ¿¢¼¿ jexcelapi test
- */
- public class ExcelReadTest
- {
- @Test
- public void excelRead() throws FileNotFoundException, IOException
- {
- List<String> paths = Arrays.asList("C:/abc.xlsx", "C:/abc.xls");
- for (String path : paths)
- {
- Workbook tempWorkbook;
- if (path.endsWith(".xls"))
- {
- tempWorkbook = new HSSFWorkbook(new FileInputStream(path));
- }
- else if (path.endsWith(".xlsx"))
- {
- tempWorkbook = new XSSFWorkbook(new FileInputStream(path));
- }
- else
- {
- throw new IllegalAccessError("xls / xlsx È®ÀåÀÚ¸¸ ÀÐÀ» ¼ö ÀÖ½À´Ï´Ù.");
- }
- try (Workbook workbook = tempWorkbook)
- {
- System.out.println("°æ·Î : " + path);
- Sheet sheet = workbook.getSheetAt(0);
- System.out.println("ù¹ø° sheet ÀÐÀ½");
- // ÇàÀ» °¡Á®¿É´Ï´Ù.
- for (Row row : sheet)
- {
- // cell (ÇàÀÇ °¢ ¿) À» °¡Á®¿É´Ï´Ù.
- row.forEach(cell ->
- {
- System.out.print(cell.toString());
- System.out.print(" || ");
- });
- System.out.println();
- }
- }
- }
- }
- }
°æ·Î : 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 ¸¦ ½áº¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.°á°ú :
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.List;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.DataFormatter;
- 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.xssf.usermodel.XSSFWorkbook;
- import org.junit.Test;
- /**
- * ¿¢¼¿ jexcelapi test
- */
- public class ExcelReadTest
- {
- @Test
- public void excelRead() throws FileNotFoundException, IOException
- {
- List<String> paths = Arrays.asList("C:/abc.xlsx", "C:/abc.xls");
- // µ¥ÀÌÅÍ Æ÷¸äÅÍ
- DataFormatter formatter = new DataFormatter();
- for (String path : paths)
- {
- Workbook tempWorkbook;
- if (path.endsWith(".xls"))
- {
- tempWorkbook = new HSSFWorkbook(new FileInputStream(path));
- }
- else if (path.endsWith(".xlsx"))
- {
- tempWorkbook = new XSSFWorkbook(new FileInputStream(path));
- }
- else
- {
- throw new IllegalAccessError("xls / xlsx È®ÀåÀÚ¸¸ ÀÐÀ» ¼ö ÀÖ½À´Ï´Ù.");
- }
- try (Workbook workbook = tempWorkbook)
- {
- System.out.println("°æ·Î : " + path);
- Sheet sheet = workbook.getSheetAt(0);
- System.out.println("ù¹ø° sheet ÀÐÀ½");
- // ÇàÀ» °¡Á®¿É´Ï´Ù.
- for (Row row : sheet)
- {
- // cell (ÇàÀÇ °¢ ¿) À» °¡Á®¿É´Ï´Ù.
- row.forEach(cell ->
- {
- // µ¥ÀÌÅÍ Æ÷¸äÅÍ »ç¿ë.
- System.out.print(formatter.formatCellValue(cell));
- System.out.print(" || ");
- });
- System.out.println();
- }
- }
- }
- }
- }
°æ·Î : 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¸Å¿ì ´ÙÇàÀ̵µ À§Ã³·³ ó¸®ÇÒ ÇÊ¿ä¾øÀÌ poi ÀÇ HSSFDateUtil ¿¡ ¾Æ·¡¿Í °°ÀÌ ÀÛ¼ºµÈ ºÎºÐÀÌ ÀÖ½À´Ï´Ù.
- int dataFormat = cell.getCellStyle().getDataFormat();
- if (dataFormat == 14) {
- dateFmt = "dd/mm/yyyy";
- } else if(dataFormat == 165) {
- dateFmt = "m/d/yy";
- } else if(dataFormat == 166) {
- dateFmt = "d-mmm-yy";
- } else if(dataFormat == 167) {
- dateFmt = "mmmm d yyyy ";
- } else if(dataFormat == 168) {
- dateFmt = "m/d/yyyy";
- } else if(dataFormat == 169) {
- dateFmt = "d-mmm-yyyy";
- }
±×·³ Äڵ带 °íÃĺ¸°Ú½À´Ï´Ù.
- /**
- * Given a format ID this will check whether the format represents
- * an internal excel date format or not.
- * @see #isADateFormat(int, java.lang.String)
- */
- public static boolean isInternalDateFormat(int format) {
- switch(format) {
- // Internal Date Formats as described on page 427 in
- // Microsoft Excel Dev's Kit...
- case 0x0e:
- case 0x0f:
- case 0x10:
- case 0x11:
- case 0x12:
- case 0x13:
- case 0x14:
- case 0x15:
- case 0x16:
- case 0x2d:
- case 0x2e:
- case 0x2f:
- return true;
- }
- return false;
- }
³¯Â¥ ¿¹¿Ü ó¸®Ãâ·Â :
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Arrays;
- import java.util.List;
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.DataFormatter;
- 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.xssf.usermodel.XSSFWorkbook;
- import org.junit.Test;
- /**
- * ¿¢¼¿ jexcelapi test
- */
- public class ExcelReadTest
- {
- @Test
- public void excelRead() throws FileNotFoundException, IOException
- {
- List<String> paths = Arrays.asList("C:/abc.xlsx", "C:/abc.xls");
- // µ¥ÀÌÅÍ Æ÷¸äÅÍ
- DataFormatter formatter = new DataFormatter();
- // µ¥ÀÌÆ® Æ÷¸Ë
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- for (String path : paths)
- {
- Workbook tempWorkbook;
- if (path.endsWith(".xls"))
- {
- tempWorkbook = new HSSFWorkbook(new FileInputStream(path));
- }
- else if (path.endsWith(".xlsx"))
- {
- tempWorkbook = new XSSFWorkbook(new FileInputStream(path));
- }
- else
- {
- throw new IllegalAccessError("xls / xlsx È®ÀåÀÚ¸¸ ÀÐÀ» ¼ö ÀÖ½À´Ï´Ù.");
- }
- try (Workbook workbook = tempWorkbook)
- {
- System.out.println("°æ·Î : " + path);
- Sheet sheet = workbook.getSheetAt(0);
- System.out.println("ù¹ø° sheet ÀÐÀ½");
- // ÇàÀ» °¡Á®¿É´Ï´Ù.
- for (Row row : sheet)
- {
- // cell (ÇàÀÇ °¢ ¿) À» °¡Á®¿É´Ï´Ù.
- row.forEach(cell ->
- {
- String value;
- // ³¯Â¥Çü ¿¹¿Ü
- if (HSSFDateUtil.isInternalDateFormat(cell.getCellStyle().getDataFormat()))
- {
- value = sdf.format(cell.getDateCellValue());
- }
- // ±âŸ
- else
- {
- value = formatter.formatCellValue(cell);
- }
- System.out.print(value);
- System.out.print(" || ");
- });
- System.out.println();
- }
- }
- }
- }
- }
°æ·Î : 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 ||
°á·Ð
ÂòÂòÇÑ ÆÁÀÌÁö¸¸... Á¶±ÝÀÌ¶óµµ µµ¿òÀÌ µÇ¼ÌÀ¸¸é ÇÕ´Ï´Ù.!!