尝试将 html 表单数据提交到 Excel

我成功地编写了将html表单数据作为单个excel行提交的代码。但是在我的html表单中,我想提交多个数据,这些数据应该附加到excel的单个单元格中,但数据的第二部分应该以同一单元格中的新行开头。就像(ALT + 输入操作)。我的html代码如下。


索引.html


{{template "header"}}

<form method="POST">

    <label for="name">NAME</label>

    <input type="text" id="name" name="name">

    <br style="mso-data-placement:same-cell;">

    <label for="gender">GENDER</label>

    <input type="text" id="gender" name="gender">

    <br>

    <label for="age">AGE</label>

    <input type="text" id="age" name="age">

    <br>

  <input type="submit">

</form>

<br>

我尝试使用“style=”mso-data-placement:same-cell;“,但它无济于事。


我的golang代码如下。


func datasubmit(w http.ResponseWriter, req *http.Request) {

      nm := req.FormValue("name")

      ge := req.FormValue("gender")

      ag := req.FormValue("age")

      var data = M{"Name": nm, "GENDER": ge, "AGE": ag}  // i have used map type M map[string]interface{}

      tpl.ExecuteTemplate(w, "index.gohtml", data)

      f, err := excelize.OpenFile("./Book99.xlsx")

          if err != nil {

          fmt.Println(err)

          return

         }

      sheetName := f.GetSheetName(0)

      fmt.Println(sheetName)

      rows, err := f.GetRows(sheetName)

      rlast := len(rows)

      fmt.Println(rows)

      fmt.Println(rlast)

      f.SetCellValue(sheetName, fmt.Sprintf("A%d", rlast+1), data["Name"])

      f.SetCellValue(sheetName, fmt.Sprintf("B%d", rlast+1), data["GENDER"])

      f.SetCellValue(sheetName, fmt.Sprintf("C%d", rlast+1), data["AGE"])

      err = f.SaveAs("./Book99.xlsx")

       if err != nil {

        fmt.Println(err)

       }

}

你能指导我打破第一个单元格与多个数据从新行开始。ex jim;jam ==> jim 既在单个单元格中,又在 excel 中用新行卡住。


长风秋雁
浏览 151回答 2
2回答

哆啦的时光机

感谢您的反馈和时间来解决我的查询。最后,我借助以下代码实现了我的目标。它有点复杂,但它对我有用,我希望它对其他人也有帮助。func datasubmit(w http.ResponseWriter, req *http.Request) {&nbsp; &nbsp; nm := req.FormValue("name")&nbsp; &nbsp; ge := req.FormValue("gender")&nbsp; &nbsp; ag := req.FormValue("age")&nbsp; &nbsp; sliNm := strings.Split(nm, ";") // creating slice if ";" occur then it treat as another element of slice&nbsp; &nbsp; var final string // again creating string to fill "\n"&nbsp; &nbsp; for _, v := range sliNm {&nbsp; &nbsp; &nbsp; &nbsp; final += v&nbsp; &nbsp; &nbsp; &nbsp; final = final + "\n"&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(final)&nbsp; &nbsp; var data = M{"Name": final, "GENDER": ge, "AGE": ag} // data as map&nbsp; &nbsp; tpl.ExecuteTemplate(w, "index.gohtml", data)&nbsp; &nbsp; f, err := excelize.OpenFile("./Book99.xlsx")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; sheetName := f.GetSheetName(0)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Set cell A1 to Wrapp&nbsp; &nbsp; style, err := f.NewStyle(&excelize.Style{&nbsp; &nbsp; &nbsp; &nbsp; Alignment: &excelize.Alignment{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WrapText: true,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; })&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(sheetName)&nbsp; &nbsp; rows, err := f.GetRows(sheetName)&nbsp; &nbsp; rlast := len(rows)&nbsp; &nbsp; fmt.Println(rows)&nbsp; &nbsp; fmt.Println(rlast)&nbsp; &nbsp; f.SetCellStyle(sheetName, fmt.Sprintf("A%d", rlast+1), fmt.Sprintf("A%d", rlast+1), style) // setting cell style for newly creating cell with every iteration&nbsp; &nbsp; fmt.Println(data["Name"])&nbsp; &nbsp; f.SetCellValue(sheetName, fmt.Sprintf("A%d", rlast+1), data["Name"])&nbsp; &nbsp; f.SetCellValue(sheetName, fmt.Sprintf("B%d", rlast+1), data["GENDER"])&nbsp; &nbsp; f.SetCellValue(sheetName, fmt.Sprintf("C%d", rlast+1), data["AGE"])&nbsp; &nbsp; err = f.SaveAs("./Book99.xlsx")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; }}此代码适用于我。假设输入是 jim;jam;==>我的o / p在excel的单个单元格中如下所示。吉姆果酱另外,是的,执行模板是没有意义的,但我创建它只是为了在网站上查看最近提交的数据。

慕森王

如果我正确阅读了您的查询,我相信您正在询问如何将新文本行添加到Excel单元格中(您将在Excel应用程序中使用ALT + Enter执行此操作)。数据的来源似乎与您的问题无关。在 Excel(在 Windows 上)中,单元格中的多行由换行符(字符0xA或转到中)拆分。但是,当您添加换行符时,Excel会自动将单元格设置为“换行”(如果不这样做,则换行符存在但不显示)中,这有点复杂。\n以下是如何在Go中实现的示例;每次运行应用程序时,它都会向单元格 A1 添加一个新行。请注意,我没有更改单元格高度,因此您需要执行此操作以使额外的行可见:func main() {&nbsp; &nbsp; f, err := excelize.OpenFile("./Book99.xlsx")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; sheetName := f.GetSheetName(0)&nbsp; &nbsp; // Set cell A1 to Wrap&nbsp; &nbsp; style, err := f.NewStyle(&excelize.Style{&nbsp; &nbsp; &nbsp; &nbsp; Alignment: &excelize.Alignment{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WrapText: true,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; })&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; if err := f.SetCellStyle("Sheet1", "A1", "A1", style); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; // Get cell value, add new name and save&nbsp; &nbsp; v, err := f.GetCellValue(sheetName, "a1")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("%x\n", v) // Output as hex so you can see how the line break is encoded&nbsp; &nbsp; err = f.SetCellValue(sheetName, "a1", v + "\n" + "Joe Bloggs")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; err = f.SaveAs("./Book99.xlsx")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go