Node.js를 사용하여 Excel에서 피벗 테이블을 만드는 방법

Node.js를 사용하여 Excel에서 피벗 테이블을 만드는 방법

Excel 파일의 가장 좋은 점은 모든 데이터 시각화 또는 컴파일 단계에서 수정할 수 있다는 것입니다. MS Excel은 데이터를 구성하고 복잡한 계산을 수행하는 데 있어 여전히 상위 목록에 있습니다. 이 블로그 게시물에서는 프로그래밍 방식으로 Node.js를 사용하여 Excel에서 피벗 테이블을 만드는 방법을 배웁니다. 또한 피벗 테이블을 기반으로 Excel 파일에서 피벗 차트를 만드는 방법도 살펴봅니다. 이를 위해 Node.js 프로젝트에 이 Excel JavaScript API를 설치합니다.

다음 섹션을 다룹니다.

엑셀 자바스크립트 API 설치

이 강력한 라이브러리를 설치하려면 API 패키지를 다운로드하거나 다음 명령을 실행하여 설치합니다.

npm install java
npm install aspose.cells

Node.js를 사용하여 Excel에서 피벗 테이블을 만드는 방법

이 섹션에서는 프로그래밍 방식으로 Excel에서 피벗 테이블을 만드는 다음 단계와 코드 조각을 작성합니다.

다음 단계와 코드 조각에 따라 Excel을 C#의 DataTable로 변환할 수 있습니다.

  1. Workbook 클래스의 인스턴스를 인스턴스화합니다.
  2. get(index) 메서드를 호출하여 첫 번째 워크시트의 참조를 가져옵니다.
  3. setName 메서드를 호출하여 워크시트의 이름을 설정합니다.
  4. getCells 메서드를 호출하여 Cell 컬렉션을 가져옵니다.
  5. Get 지정된 셀 이름에 있는 Cell 요소입니다.
  6. setValue 메서드를 호출하여 범위 값을 설정합니다.
  7. get 메서드를 호출하여 범위의 Cell 객체를 가져옵니다.
  8. add 메서드를 호출하여 새 시트를 추가합니다.
  9. get 지정된 인덱스에 있는 워크시트 요소입니다.
  10. setName 메서드를 호출하여 시트의 이름을 지정합니다.
  11. add 메서드를 호출하여 워크시트에 피벗 테이블을 추가합니다.
  12. setRowGrand 메소드의 값을 설정하여 총합계를 표시합니다.
  13. 피벗 테이블 보고서에 열에 대한 총합계가 표시되는지 여부를 나타내는 setColumnGrand 메서드 값을 설정합니다.
  14. 이제 피벗 테이블 보고서의 서식이 자동으로 지정되는지 여부를 나타내는 setAutoFormat 메서드의 값을 설정합니다.
  15. setAutoFormatType 메서드를 호출하여 피벗 테이블 autoformat 유형을 설정합니다.
  16. addFieldToArea 메서드를 호출하여 첫 번째, 두 번째, 세 번째, 네 번째 및 다섯 번째 필드를 행 영역으로 끕니다.
  17. getDataFields 메서드를 호출하여 첫 번째 데이터 필드의 숫자 형식을 설정합니다.
  18. save 메서드를 호출하여 Excel 파일을 저장합니다.

다음 코드를 복사하여 기본 파일에 붙여넣습니다.

var aspose = aspose || {};
aspose.cells = require("aspose.cells");
// Workbook 클래스의 인스턴스화 
var workbook = new aspose.cells.Workbook();
// get(index) 메서드를 호출하여 첫 번째 워크시트의 참조를 가져옵니다. 
var sheet = workbook.getWorksheets().get(0);
//  setName 메소드를 호출하여 워크시트의 이름을 설정하십시오. 
sheet.setName("Data");
// getCells 메소드를 호출하여 Cell 컬렉션을 가져옵니다. 
var cells = sheet.getCells();

// 지정된 셀 이름에서 Cell 요소를 가져옵니다. 
var cell = cells.get("A1");
// 이 메서드를 호출하여 범위 값을 설정합니다. 
cell.setValue("Employee");
// get 메소드를 호출하여 범위의 Cell 객체를 가져옵니다.
cell = cells.get("B1");
cell.setValue("Quarter");
cell = cells.get("C1");
cell.setValue("Product");
cell = cells.get("D1");
cell.setValue("Continent");
cell = cells.get("E1");
cell.setValue("Country");
cell = cells.get("F1");
cell.setValue("Sale");

cell = cells.get("A2");
cell.setValue("David");
cell = cells.get("A3");
cell.setValue("David");
cell = cells.get("A4");
cell.setValue("David");
cell = cells.get("A5");
cell.setValue("David");
cell = cells.get("A6");
cell.setValue("James");
cell = cells.get("A7");
cell.setValue("James");
cell = cells.get("A8");
cell.setValue("James");
cell = cells.get("A9");
cell.setValue("James");
cell = cells.get("A10");
cell.setValue("James");
cell = cells.get("A11");
cell.setValue("Miya");
cell = cells.get("A12");
cell.setValue("Miya");
cell = cells.get("A13");
cell.setValue("Miya");
cell = cells.get("A14");
cell.setValue("Miya");
cell = cells.get("A15");
cell.setValue("Miya");
cell = cells.get("A16");
cell.setValue("Miya");
cell = cells.get("A17");
cell.setValue("Miya");
cell = cells.get("A18");
cell.setValue("Elvis");
cell = cells.get("A19");
cell.setValue("Elvis");
cell = cells.get("A20");
cell.setValue("Elvis");
cell = cells.get("A21");
cell.setValue("Elvis");
cell = cells.get("A22");
cell.setValue("Elvis");
cell = cells.get("A23");
cell.setValue("Elvis");
cell = cells.get("A24");
cell.setValue("Elvis");
cell = cells.get("A25");
cell.setValue("Jean");
cell = cells.get("A26");
cell.setValue("Jean");
cell = cells.get("A27");
cell.setValue("Jean");
cell = cells.get("A28");
cell.setValue("Ada");
cell = cells.get("A29");
cell.setValue("Ada");
cell = cells.get("A30");
cell.setValue("Ada");

cell = cells.get("B2");
cell.setValue("1");
cell = cells.get("B3");
cell.setValue("2");
cell = cells.get("B4");
cell.setValue("3");
cell = cells.get("B5");
cell.setValue("4");
cell = cells.get("B6");
cell.setValue("1");
cell = cells.get("B7");
cell.setValue("2");
cell = cells.get("B8");
cell.setValue("3");
cell = cells.get("B9");
cell.setValue("4");
cell = cells.get("B10");
cell.setValue("4");
cell = cells.get("B11");
cell.setValue("1");
cell = cells.get("B12");
cell.setValue("1");
cell = cells.get("B13");
cell.setValue("2");
cell = cells.get("B14");
cell.setValue("2");
cell = cells.get("B15");
cell.setValue("3");
cell = cells.get("B16");
cell.setValue("4");
cell = cells.get("B17");
cell.setValue("4");
cell = cells.get("B18");
cell.setValue("1");
cell = cells.get("B19");
cell.setValue("1");
cell = cells.get("B20");
cell.setValue("2");
cell = cells.get("B21");
cell.setValue("3");
cell = cells.get("B22");
cell.setValue("3");
cell = cells.get("B23");
cell.setValue("4");
cell = cells.get("B24");
cell.setValue("4");
cell = cells.get("B25");
cell.setValue("1");
cell = cells.get("B26");
cell.setValue("2");
cell = cells.get("B27");
cell.setValue("3");
cell = cells.get("B28");
cell.setValue("1");
cell = cells.get("B29");
cell.setValue("2");
cell = cells.get("B30");
cell.setValue("3");

cell = cells.get("C2");
cell.setValue("Maxilaku");
cell = cells.get("C3");
cell.setValue("Maxilaku");
cell = cells.get("C4");
cell.setValue("Chai");
cell = cells.get("C5");
cell.setValue("Maxilaku");
cell = cells.get("C6");
cell.setValue("Chang");
cell = cells.get("C7");
cell.setValue("Chang");
cell = cells.get("C8");
cell.setValue("Chang");
cell = cells.get("C9");
cell.setValue("Chang");
cell = cells.get("C10");
cell.setValue("Chang");
cell = cells.get("C11");
cell.setValue("Geitost");
cell = cells.get("C12");
cell.setValue("Chai");
cell = cells.get("C13");
cell.setValue("Geitost");
cell = cells.get("C14");
cell.setValue("Geitost");
cell = cells.get("C15");
cell.setValue("Maxilaku");
cell = cells.get("C16");
cell.setValue("Geitost");
cell = cells.get("C17");
cell.setValue("Geitost");
cell = cells.get("C18");
cell.setValue("Ikuru");
cell = cells.get("C19");
cell.setValue("Ikuru");
cell = cells.get("C20");
cell.setValue("Ikuru");
cell = cells.get("C21");
cell.setValue("Ikuru");
cell = cells.get("C22");
cell.setValue("Ipoh Coffee");
cell = cells.get("C23");
cell.setValue("Ipoh Coffee");
cell = cells.get("C24");
cell.setValue("Ipoh Coffee");
cell = cells.get("C25");
cell.setValue("Chocolade");
cell = cells.get("C26");
cell.setValue("Chocolade");
cell = cells.get("C27");
cell.setValue("Chocolade");
cell = cells.get("C28");
cell.setValue("Chocolade");
cell = cells.get("C29");
cell.setValue("Chocolade");
cell = cells.get("C30");
cell.setValue("Chocolade");

cell = cells.get("D2");
cell.setValue("Asia");
cell = cells.get("D3");
cell.setValue("Asia");
cell = cells.get("D4");
cell.setValue("Asia");
cell = cells.get("D5");
cell.setValue("Asia");
cell = cells.get("D6");
cell.setValue("Europe");
cell = cells.get("D7");
cell.setValue("Europe");
cell = cells.get("D8");
cell.setValue("Europe");
cell = cells.get("D9");
cell.setValue("Europe");
cell = cells.get("D10");
cell.setValue("Europe");
cell = cells.get("D11");
cell.setValue("America");
cell = cells.get("D12");
cell.setValue("America");
cell = cells.get("D13");
cell.setValue("America");
cell = cells.get("D14");
cell.setValue("America");
cell = cells.get("D15");
cell.setValue("America");
cell = cells.get("D16");
cell.setValue("America");
cell = cells.get("D17");
cell.setValue("America");
cell = cells.get("D18");
cell.setValue("Europe");
cell = cells.get("D19");
cell.setValue("Europe");
cell = cells.get("D20");
cell.setValue("Europe");
cell = cells.get("D21");
cell.setValue("Oceania");
cell = cells.get("D22");
cell.setValue("Oceania");
cell = cells.get("D23");
cell.setValue("Oceania");
cell = cells.get("D24");
cell.setValue("Oceania");
cell = cells.get("D25");
cell.setValue("Africa");
cell = cells.get("D26");
cell.setValue("Africa");
cell = cells.get("D27");
cell.setValue("Africa");
cell = cells.get("D28");
cell.setValue("Africa");
cell = cells.get("D29");
cell.setValue("Africa");
cell = cells.get("D30");
cell.setValue("Africa");

cell = cells.get("E2");
cell.setValue("China");
cell = cells.get("E3");
cell.setValue("India");
cell = cells.get("E4");
cell.setValue("Korea");
cell = cells.get("E5");
cell.setValue("India");
cell = cells.get("E6");
cell.setValue("France");
cell = cells.get("E7");
cell.setValue("France");
cell = cells.get("E8");
cell.setValue("Germany");
cell = cells.get("E9");
cell.setValue("Italy");
cell = cells.get("E10");
cell.setValue("France");
cell = cells.get("E11");
cell.setValue("U.S.");
cell = cells.get("E12");
cell.setValue("U.S.");
cell = cells.get("E13");
cell.setValue("Brazil");
cell = cells.get("E14");
cell.setValue("U.S.");
cell = cells.get("E15");
cell.setValue("U.S.");
cell = cells.get("E16");
cell.setValue("Canada");
cell = cells.get("E17");
cell.setValue("U.S.");
cell = cells.get("E18");
cell.setValue("Italy");
cell = cells.get("E19");
cell.setValue("France");
cell = cells.get("E20");
cell.setValue("Italy");
cell = cells.get("E21");
cell.setValue("New Zealand");
cell = cells.get("E22");
cell.setValue("Australia");
cell = cells.get("E23");
cell.setValue("Australia");
cell = cells.get("E24");
cell.setValue("New Zealand");
cell = cells.get("E25");
cell.setValue("S.Africa");
cell = cells.get("E26");
cell.setValue("S.Africa");
cell = cells.get("E27");
cell.setValue("S.Africa");
cell = cells.get("E28");
cell.setValue("Egypt");
cell = cells.get("E29");
cell.setValue("Egypt");
cell = cells.get("E30");
cell.setValue("Egypt");

cell = cells.get("F2");
cell.setValue(2000);
cell = cells.get("F3");
cell.setValue(500);
cell = cells.get("F4");
cell.setValue(1200);
cell = cells.get("F5");
cell.setValue(1500);
cell = cells.get("F6");
cell.setValue(500);
cell = cells.get("F7");
cell.setValue(1500);
cell = cells.get("F8");
cell.setValue(800);
cell = cells.get("F9");
cell.setValue(900);
cell = cells.get("F10");
cell.setValue(500);
cell = cells.get("F11");
cell.setValue(1600);
cell = cells.get("F12");
cell.setValue(600);
cell = cells.get("F13");
cell.setValue(2000);
cell = cells.get("F14");
cell.setValue(500);
cell = cells.get("F15");
cell.setValue(900);
cell = cells.get("F16");
cell.setValue(700);
cell = cells.get("F17");
cell.setValue(1400);
cell = cells.get("F18");
cell.setValue(1350);
cell = cells.get("F19");
cell.setValue(300);
cell = cells.get("F20");
cell.setValue(500);
cell = cells.get("F21");
cell.setValue(1000);
cell = cells.get("F22");
cell.setValue(1500);
cell = cells.get("F23");
cell.setValue(1500);
cell = cells.get("F24");
cell.setValue(1600);
cell = cells.get("F25");
cell.setValue(1000);
cell = cells.get("F26");
cell.setValue(1200);
cell = cells.get("F27");
cell.setValue(1300);
cell = cells.get("F28");
cell.setValue(1500);
cell = cells.get("F29");
cell.setValue(1400);
cell = cells.get("F30");
cell.setValue(1000);


// add 메소드를 호출하여 새 시트 추가 
var sheetIndex = workbook.getWorksheets().add();
// 지정된 인덱스에서 Worksheet 요소를 가져옵니다. 
var sheet2 = workbook.getWorksheets().get(sheetIndex);
// setName 메서드를 호출하여 시트 이름 지정 
sheet2.setName("PivotTable");
// 시트에서 피벗 테이블 컬렉션 가져오기
var pivotTables = sheet2.getPivotTables();
// add 메서드를 호출하여 워크시트에 피벗 테이블 추가 
var index = pivotTables.add("=Data!A1:F30", "B3", "PivotTable1");
// 새로 추가된 피벗 테이블의 인스턴스를 가져옵니다.
var pivotTable = pivotTables.get(index);
// setRowGrand 메소드의 값을 설정하여 총합계 표시 
pivotTable.setRowGrand(true);
// 피벗 테이블 보고서에 열에 대한 총합계가 표시되는지 여부를 나타내는 setColumnGrand 메서드 값을 설정합니다.
pivotTable.setColumnGrand(true);
// 피벗 테이블 보고서의 서식이 자동으로 지정되는지 여부를 나타내는 setAutoFormat 메서드의 값을 설정합니다. 
pivotTable.setAutoFormat(true);
// setAutoFormatType 메서드를 호출하여 피벗 테이블 자동 서식 유형을 설정합니다. 
pivotTable.setAutoFormatType(aspose.cells.PivotTableAutoFormatType.REPORT_6);
// addFieldToArea 메소드를 호출하여 첫 번째, 두 번째, 세 번째, 네 번째 및 다섯 번째 필드를 행 영역으로 끕니다. 
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.ROW, 0);
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.ROW, 2);
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.ROW, 1);
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.COLUMN, 3);
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.DATA, 5);
// getDataFields 메소드를 호출하여 첫 번째 데이터 필드의 숫자 형식 설정 
pivotTable.getDataFields().get(0).setNumber(7);
// save 메소드를 호출하여 Excel 파일 저장 
workbook.save(  "pivotTable_test.xls");

아래 이미지에서 출력을 볼 수 있습니다.

Node.js를 사용하여 Excel에서 피벗 테이블 만들기

프로그래밍 방식으로 Excel에서 피벗 차트를 만드는 방법

이제 이 Excel JavaScript API를 사용하여 생성된 피벗 테이블을 기반으로 피벗 차트를 만드는 기능을 구현합니다.

아래에 언급된 단계와 코드 스니펫을 따를 수 있습니다.

  1. Workbook 클래스의 인스턴스를 인스턴스화합니다.
  2. 컬렉션에 워크시트를 add하려면 add 메서드를 호출합니다.
  3. get 지정된 인덱스에 있는 워크시트 요소입니다.
  4. setName 메서드를 호출하여 시트의 이름을 지정합니다.
  5. add 메서드를 호출하여 세로 막대형 차트를 추가합니다.
  6. get 지정된 인덱스에 있는 Chart 요소입니다.
  7. setPivotSource 메서드를 호출하여 피벗 차트 데이터 소스를 설정합니다.
  8. 차트가 PivotChart인 경우에만 피벗 차트 필드 버튼을 숨기려면 setHidePivotFieldButtons 메서드를 호출합니다.
  9. save 엑셀 파일입니다.

다음 코드를 복사하여 기본 파일에 붙여넣습니다.

var aspose = aspose || {};
aspose.cells = require("aspose.cells");
// Workbook 클래스의 인스턴스 만들기 
var workbook = new aspose.cells.Workbook("pivotTable_test.xls");
// add 메서드를 호출하여 컬렉션에 워크시트를 추가합니다.
var sheetIndex = workbook.getWorksheets().add(aspose.cells.SheetType.CHART);
// 지정된 인덱스에서 Worksheet 요소를 가져옵니다. 
var sheet3 = workbook.getWorksheets().get(sheetIndex);
// setName 메서드를 호출하여 시트 이름 지정 
sheet3.setName("PivotChart");
// add 메서드를 호출하여 세로 막대형 차트 추가 
var chartIndex = sheet3.getCharts().add(aspose.cells.ChartType.COLUMN, 0, 5, 28, 16);
// 지정된 인덱스에서 Chart 요소를 가져옵니다. 
var chart = sheet3.getCharts().get(chartIndex);
// setPivotSource 메서드를 호출하여 피벗 차트 데이터 소스를 설정합니다. 
chart.setPivotSource("PivotTable!PivotTable1");
// 차트가 PivotChart인 경우에만 피벗 차트 필드 버튼을 숨기려면 setHidePivotFieldButtons 메소드를 호출하십시오. 
chart.setHidePivotFieldButtons(false);
// 엑셀 파일 저장 
workbook.save( "pivotChart_test.xls");

출력은 아래 이미지에 표시됩니다.

프로그래밍 방식으로 Excel에서 피벗 차트를 만드는 방법

무료 라이선스 받기

무료 임시 라이선스를 사용하여 평가 제한 없이 API를 사용할 수 있습니다.

합산

이 엔터프라이즈 수준의 Excel JavaScript API를 살펴보았듯이. 또한 프로그래밍 방식으로 Node.js를 사용하여 Excel에서 피벗 테이블을 만드는 방법을 배웠고 피벗 차트를 만드는 기능을 구현했습니다. 또한 문서를 방문하여 다른 기능을 알 수 있습니다.

또한 시작 안내서를 따르는 것이 좋습니다.

마지막으로, conholdate.com은 지속적으로 새로운 블로그 게시물을 작성하고 있습니다. 따라서 최신 업데이트에 대한 연락을 유지하십시오.

질문하기

포럼에서 질문이나 질문에 대해 알려주실 수 있습니다.

자주 묻는 질문

Excel에서 피벗 테이블을 만들고 편집하는 방법은 무엇입니까?

이 Excel JavaScript API를 설치하여 프로그래밍 방식으로 Excel에서 피벗 테이블을 만들 수 있습니다. 또한 이 링크를 방문하여 단계와 코드 조각을 얻을 수 있습니다.

또한보십시오