当前位置: 首页 > news >正文

大学专业网站免费发布活动的平台

大学专业网站,免费发布活动的平台,淮北人论坛招聘网,常州城乡建设学校网站最近项目开发中有涉及到Excel的导入与导出功能,特别是导出表格时需要特定的格式(单元格合并等),废话不多说,直接上代码了。 首先用到一个第三方库,实测还是很强大很好用的,就是这个https://git…

最近项目开发中有涉及到Excel的导入与导出功能,特别是导出表格时需要特定的格式(单元格合并等),废话不多说,直接上代码了。

首先用到一个第三方库,实测还是很强大很好用的,就是这个https://github.com/qax-os/excelize

引用库
go get github.com/xuri/excelize/v2
导入表格
package mainimport ("encoding/json""github.com/xuri/excelize/v2""net/http""strings"
)func fileImport(w http.ResponseWriter, r *http.Request) {// 获取请求中的文件名formFile, _, err := r.FormFile("filename")if err != nil {w.Write([]byte("获取文件失败, " + err.Error()))return}// 关闭defer formFile.Close()//reader, err := excelize.OpenReader(formFile)if err != nil {w.Write([]byte("读取文件失败, " + err.Error()))return}// 关闭defer reader.Close()rows, err := reader.GetRows("Sheet1")if err != nil {w.Write([]byte("获取工作表失败, " + err.Error()))return}ret := make([]string, 0, 8)for i, row := range rows {// 每一行数据的列, 都是从0开始的, 一般0行都是表头if i == 0 {continue}value1 := row[0] // 第一列value2 := row[1] // 第二列// 去除空格value1 = strings.Trim(strings.TrimSpace(value1), "\n")value2 = strings.Trim(strings.TrimSpace(value2), "\n")//ret = append(ret, value1+","+value2)}bytes, _ := json.Marshal(ret)w.Write(bytes)return
}func main() {// HTTP服务http.HandleFunc("/fileImport", fileImport)err := http.ListenAndServe(":8192", nil)if err != nil {panic(err)}
}

表格格式
在这里插入图片描述
执行后效果
在这里插入图片描述

导出表格
package mainimport ("encoding/json""fmt""github.com/xuri/excelize/v2""net/http""strings"
)func fileExport(w http.ResponseWriter, r *http.Request) {file := excelize.NewFile()defer file.Close()// 设置页sheetName := "Sheet1"// 创建sheet, err := file.NewSheet(sheetName)if err != nil {w.Write([]byte("创建失败, " + err.Error()))return}// 设置单元格格式style := &excelize.Style{Border: nil,Fill:   excelize.Fill{},Font:   nil,Alignment: &excelize.Alignment{Horizontal:      "center",Indent:          0,JustifyLastLine: false,ReadingOrder:    0,RelativeIndent:  0,ShrinkToFit:     false,TextRotation:    0,Vertical:        "center",WrapText:        false,},Protection:    nil,NumFmt:        0,DecimalPlaces: nil,CustomNumFmt:  nil,NegRed:        false,}styleID, _ := file.NewStyle(style)// 设置表头_ = file.SetCellValue(sheetName, "A1", "款")_ = file.SetCellStyle(sheetName, "A1", "A1", styleID)_ = file.SetCellValue(sheetName, "B1", "尺码")_ = file.SetCellStyle(sheetName, "B1", "B1", styleID)// 设置值for i := 0; i < 5; i++ {lineStr := fmt.Sprintf("%d", i+2)//_ = file.SetCellValue(sheetName, "A"+lineStr, "基础款")_ = file.SetCellStyle(sheetName, "A"+lineStr, "A"+lineStr, styleID)//_ = file.SetCellValue(sheetName, "B"+lineStr, "1:2:3:4:5:6")_ = file.SetCellStyle(sheetName, "B"+lineStr, "B"+lineStr, styleID)}//file.SetActiveSheet(sheet)//buffer, err := file.WriteToBuffer()if err != nil {w.Write([]byte("导出失败, " + err.Error()))return}w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "导出文件.xlsx"))w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")w.Write(buffer.Bytes())
}func main() {// HTTP服务http.HandleFunc("/fileImport", fileImport)http.HandleFunc("/fileExport", fileExport)err := http.ListenAndServe(":8192", nil)if err != nil {panic(err)}
}

页面请求 http://127.0.0.1:8192/fileExport 后会直接生成xlsx文件并下载
在这里插入图片描述

合并单元格

package mainimport ("encoding/json""fmt""github.com/xuri/excelize/v2""net/http""strings"
)func fileExport(w http.ResponseWriter, r *http.Request) {file := excelize.NewFile()defer file.Close()// 设置页sheetName := "Sheet1"// 创建sheet, err := file.NewSheet(sheetName)if err != nil {w.Write([]byte("创建失败, " + err.Error()))return}// 设置单元格格式style := &excelize.Style{Border: nil, // 边框样式Fill:   excelize.Fill{},Font:   nil, // 字体样式Alignment: &excelize.Alignment{ // 位置样式Horizontal:      "center",Indent:          0,JustifyLastLine: false,ReadingOrder:    0,RelativeIndent:  0,ShrinkToFit:     false,TextRotation:    0,Vertical:        "center",WrapText:        false,},Protection:    nil,NumFmt:        0,DecimalPlaces: nil,CustomNumFmt:  nil,NegRed:        false,}styleID, _ := file.NewStyle(style)// 设置表头_ = file.MergeCell(sheetName, "A1", "A2") // 合并单元格_ = file.SetCellValue(sheetName, "A1", "款")_ = file.SetCellStyle(sheetName, "A1", "A2", styleID)_ = file.MergeCell(sheetName, "B1", "G1") // 合并单元格_ = file.SetCellValue(sheetName, "B1", "尺码")_ = file.SetCellStyle(sheetName, "B1", "G1", styleID)_ = file.SetCellValue(sheetName, "B2", "XS")_ = file.SetCellStyle(sheetName, "B2", "B2", styleID)_ = file.SetCellValue(sheetName, "C2", "S")_ = file.SetCellStyle(sheetName, "C2", "C2", styleID)_ = file.SetCellValue(sheetName, "D2", "M")_ = file.SetCellStyle(sheetName, "D2", "D2", styleID)_ = file.SetCellValue(sheetName, "E2", "L")_ = file.SetCellStyle(sheetName, "E2", "E2", styleID)_ = file.SetCellValue(sheetName, "F2", "XL")_ = file.SetCellStyle(sheetName, "F2", "F2", styleID)_ = file.SetCellValue(sheetName, "G2", "XLL")_ = file.SetCellStyle(sheetName, "G2", "G2", styleID)// 设置值for i := 0; i < 5; i++ {lineStr := fmt.Sprintf("%d", i+3)//_ = file.SetCellValue(sheetName, "A"+lineStr, "基础款")_ = file.SetCellStyle(sheetName, "A"+lineStr, "A"+lineStr, styleID)//split := strings.Split("1:2:3:4:5:6", ":")_ = file.SetCellValue(sheetName, "B"+lineStr, split[0])_ = file.SetCellStyle(sheetName, "B"+lineStr, "B"+lineStr, styleID)_ = file.SetCellValue(sheetName, "C"+lineStr, split[1])_ = file.SetCellStyle(sheetName, "C"+lineStr, "C"+lineStr, styleID)_ = file.SetCellValue(sheetName, "D"+lineStr, split[2])_ = file.SetCellStyle(sheetName, "D"+lineStr, "D"+lineStr, styleID)_ = file.SetCellValue(sheetName, "E"+lineStr, split[3])_ = file.SetCellStyle(sheetName, "E"+lineStr, "E"+lineStr, styleID)_ = file.SetCellValue(sheetName, "F"+lineStr, split[4])_ = file.SetCellStyle(sheetName, "F"+lineStr, "F"+lineStr, styleID)_ = file.SetCellValue(sheetName, "G"+lineStr, split[5])_ = file.SetCellStyle(sheetName, "G"+lineStr, "G"+lineStr, styleID)}//file.SetActiveSheet(sheet)//buffer, err := file.WriteToBuffer()if err != nil {w.Write([]byte("导出失败, " + err.Error()))return}// 设置文件名w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "导出文件.xlsx"))// 导出的文件格式 xlsx 或者 xsl// xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet// xls application/vnd.ms-excelw.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")w.Write(buffer.Bytes())
}func main() {// HTTP服务http.HandleFunc("/fileImport", fileImport)http.HandleFunc("/fileExport", fileExport)err := http.ListenAndServe(":8192", nil)if err != nil {panic(err)}
}

页面请求 http://127.0.0.1:8192/fileExport 后会直接生成xlsx文件并下载
在这里插入图片描述

http://www.bjxfkj.com.cn/article/106865.html

相关文章:

  • js 获取 网站路径app推广员好做吗
  • 网站改版的seo注意事项淘宝关键词搜索量查询
  • 网站空间送域名深圳网站做优化哪家公司好
  • 阿拉善左旗建设局网站品牌如何推广
  • 网站建设服务费一年多少钱百度一下就知道官方
  • 网站风格对比信息表百度后台登陆入口
  • 网站建设 风险说明百度一下你就知道了百度一下
  • 全屏网站模板seo网络优化平台
  • 深圳疫情开放时间广州seo优化公司
  • 网络推广是网络营销的基础对不对青岛seo整站优化招商电话
  • 社交网站先做pc站可以吗竞价排名的优缺点
  • 做电影分享网站违法吗朋友圈广告30元 1000次
  • 网站建设app开发怎么做好网络营销
  • 最好的网站统计网站服务器
  • 课堂资源管理网站开发需求分析营销推广怎么做
  • 俄语网站开发关键词代发包收录
  • 视差设计网站sem是什么品牌
  • 2019 做网站百度关键词优化怎么做
  • 南昌做网站后台投票在线推广企业网站的方法
  • 旅游网站规划说明营销咨询
  • 域名续费做网站电工培训技术学校
  • 中关村手机之家报价河南靠谱seo电话
  • wordpress教程网视频seo外链购买
  • 郑州做网站的公司哪些注册城乡规划师好考吗
  • 兰州网站制作免费推广工具
  • 网页设计 网站建设啥意思中国十大门户网站排行
  • 做b2c网站还是平台免费个人网站制作
  • 做网站答辩广州网站定制多少钱
  • 阿里云服务器做网站安全吗全网关键词搜索排行
  • 做网站如何添加视频查找网站