Skip to content

Add files via upload #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 系统开发excel/Alldata.xlsx
Binary file not shown.
Binary file not shown.
Binary file added 系统开发excel/lib/commons-compress-1.19.jar
Binary file not shown.
Binary file added 系统开发excel/lib/xmlbeans-3.1.0.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1,120 changes: 1,120 additions & 0 deletions 系统开发excel/src/com/company/Administrator.java

Large diffs are not rendered by default.

129 changes: 129 additions & 0 deletions 系统开发excel/src/com/company/Excel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.company;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;





public class Excel {




public void create(){
String fileName = "Alldata.xlsx";
String sheetName = "user";
String[] columnHeaders = {"用户ID", "用户名", "用户密码", "类型", "用户级别", "用户注册时间", "客户累计消费总金额", "用户手机号", "用户邮箱"};

Workbook workbook = null;
try {
File file = new File(fileName);
if (!file.exists()) {
// 创建新的Excel文件
workbook = new XSSFWorkbook();
} else {
// 打开现有的Excel文件
FileInputStream fileInputStream = new FileInputStream(file);
workbook = new XSSFWorkbook(fileInputStream);
fileInputStream.close();
}

// 获取或创建表格
Sheet sheet = workbook.getSheet(sheetName);
if (sheet == null) {
sheet = workbook.createSheet(sheetName);
// 写入列头
Row headerRow = sheet.createRow(0);
for (int i = 0; i < columnHeaders.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columnHeaders[i]);
}
}

String sheetName1 = "commodity";
String[] columnHeaders1 = {"商品编号", "商品名称", "商品厂家", "生产日期", "型号", "进货价", "零售价", "数量"};
Sheet sheet1 = workbook.getSheet(sheetName1);
if (sheet1 == null) {
sheet1 = workbook.createSheet(sheetName1);
// 写入列头
Row headerRow = sheet1.createRow(0);
for (int i = 0; i < columnHeaders1.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columnHeaders1[i]);
}
}


String sheetName2 = "shopcart";
String[] columnHeaders2 = {"用户名", "商品", "价格", "数量"};
Sheet sheet2 = workbook.getSheet(sheetName2);
if (sheet2 == null) {
sheet2 = workbook.createSheet(sheetName2);
// 写入列头
Row headerRow = sheet2.createRow(0);
for (int i = 0; i < columnHeaders2.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columnHeaders2[i]);
}
}



String sheetName3 = "history";
String[] columnHeaders3 = {"用户名", "商品", "数量", "金额","购买时间"};
Sheet sheet3 = workbook.getSheet(sheetName3);
if (sheet3 == null) {
sheet3 = workbook.createSheet(sheetName3);
// 写入列头
Row headerRow = sheet3.createRow(0);
for (int i = 0; i < columnHeaders3.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columnHeaders3[i]);
}
}



// 写入数据
// Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
// DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// dataRow.createCell(0).setCellValue("123");
// dataRow.createCell(1).setCellValue("John Doe");
// dataRow.createCell(2).setCellValue("password");
// dataRow.createCell(3).setCellValue("普通用户");
// dataRow.createCell(4).setCellValue("Silver");
// dataRow.createCell(5).setCellValue(LocalDateTime.now().format(dtf));
// dataRow.createCell(6).setCellValue(5000);
// dataRow.createCell(7).setCellValue("1234567890");
// dataRow.createCell(8).setCellValue("[email protected]");

// 保存Excel文件
FileOutputStream fileOut = new FileOutputStream(fileName);
workbook.write(fileOut);
fileOut.close();
// System.out.println("Excel文件保存成功!");

} catch (IOException e) {
e.printStackTrace();
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
37 changes: 37 additions & 0 deletions 系统开发excel/src/com/company/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.company;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;






import static java.lang.System.exit;

public class Main {

public static void main(String[] args) {
// write your code here

Excel f=new Excel();
f.create();


Start s=new Start();
s.start();

}

}
47 changes: 47 additions & 0 deletions 系统开发excel/src/com/company/Start.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.company;

import java.util.InputMismatchException;
import java.util.Scanner;

import static java.lang.System.exit;

public class Start {

public void start(){

Scanner input = new Scanner(System.in);

User user=new User();
Administrator administrator=new Administrator();

boolean mis = true;


while (mis) {
try {
System.out.println("***************START***************");
System.out.print("请选择身份————1.管理员 2.用户 (按‘0’退出系统)>");
int choose = input.nextInt();

if (choose == 1) {
administrator.administratorLogin();
mis = false;
} else if (choose == 2) {
user.userLogin();
mis = false;
} else if (choose == 0) {
System.exit(1);
} else {
System.out.println("输入有误,请重新输入");
}
} catch (InputMismatchException e) {
System.out.println("输入有误,请重新输入");
input.nextLine(); // 清空输入缓冲区
}
}

}



}
Loading