简体   繁体   English

试图提交 html 表单数据到 excel

[英]Trying to submit html form data to excel

I got success to write code to submit html form data as single excel row.我成功编写代码以将 html 表单数据作为单个 excel 行提交。 But in my html form I want to submit multiple data which should be appended in single cell of excel but second part of data should start with new line in the same cell.但是在我的 html 表单中,我想提交多个数据,这些数据应该附加在 excel 的单个单元格中,但数据的第二部分应该以同一单元格中的新行开头。 Just like (ALT + Enter Operation).就像(ALT + Enter 操作)。 My html code is as below.我的 html 代码如下。

index.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>

I tried with "style="mso-data-placement:same-cell;" but it wont help.我尝试使用“style="mso-data-placement:same-cell;" 但它无济于事。

My golang code is as below.我的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)
       }
}

Can you please guide me to break first cell with multiple data start with new line.你能指导我用多个数据从新行开始打破第一个单元格吗? ex jim;jam ==> jim jam both in single cell but jam with new line in excel. ex jim;jam ==> jim jam 都在单个单元格中,但在 excel 中与新行卡住。

If I am reading your query correctly I believe you are asking how to add a new line of text into an excel cell (you would use ALT+Enter to do this in the Excel application).如果我正确阅读了您的查询,我相信您在问如何将新的文本行添加到 excel 单元格中(您可以在 Excel 应用程序中使用 ALT+Enter 来执行此操作)。 Where the data comes from does not appear to be relevant to your question.数据的来源似乎与您的问题无关。

In Excel (on Windows) multiple lines in a cell are split by a line feed (char 0xA, or \n in go).在 Excel(在 Windows 上)中,一个单元格中的多行由换行符(char 0xA 或\n in go)分割。 However there is a slight complication in that Excel automatically sets the cell to "wrap" when you add a line break (if you don't do this the line break is there but not displayed).但是,当您添加换行符时,Excel 会自动将单元格设置为“换行”(如果您不这样做,则换行符存在但不显示),这有一点复杂。

Here is an example of how this can be implemented in Go;这是如何在 Go 中实现的示例; each time you run the application it will add a new line to cell A1.每次运行应用程序时,它都会在单元格 A1 中添加一个新行。 Note that I am not changing the cell height so you will need to do that to make the extra lines visible:请注意,我没有更改单元格高度,因此您需要这样做以使额外的行可见:

func main() {
    f, err := excelize.OpenFile("./Book99.xlsx")
    if err != nil {
        panic(err)
    }
    sheetName := f.GetSheetName(0)

    // Set cell A1 to Wrap
    style, err := f.NewStyle(&excelize.Style{
        Alignment: &excelize.Alignment{
            WrapText: true,
        },
    })
    if err != nil {
        panic(err)
    }
    if err := f.SetCellStyle("Sheet1", "A1", "A1", style); err != nil {
        panic(err)
    }

    // Get cell value, add new name and save
    v, err := f.GetCellValue(sheetName, "a1")
    if err != nil {
        panic(err)
    }
    fmt.Printf("%x\n", v) // Output as hex so you can see how the line break is encoded
    err = f.SetCellValue(sheetName, "a1", v + "\n" + "Joe Bloggs")
    if err != nil {
        panic(err)
    }
    err = f.SaveAs("./Book99.xlsx")
    if err != nil {
        panic(err)
    }
}

Appreciate your feedback and time given to resolve my query.感谢您的反馈和时间来解决我的问题。

Finally, I achieved my goal with help of below code.最后,我在下面的代码的帮助下实现了我的目标。 It is slight complicated but yes its working for me and I hope it will be helpful for others too.它有点复杂,但它对我有用,我希望它对其他人也有帮助。


func datasubmit(w http.ResponseWriter, req *http.Request) {
    nm := req.FormValue("name")
    ge := req.FormValue("gender")
    ag := req.FormValue("age")

    sliNm := strings.Split(nm, ";") // creating slice if ";" occur then it treat as another element of slice

    var final string // again creating string to fill "\n"
    for _, v := range sliNm {
        final += v
        final = final + "\n"
    }

    fmt.Println(final)

    var data = M{"Name": final, "GENDER": ge, "AGE": ag} // data as map
    tpl.ExecuteTemplate(w, "index.gohtml", data)

    f, err := excelize.OpenFile("./Book99.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }

    sheetName := f.GetSheetName(0)
    
    // Set cell A1 to Wrapp
    style, err := f.NewStyle(&excelize.Style{
        Alignment: &excelize.Alignment{
            WrapText: true,
        },
    })
    if err != nil {
        panic(err)
    }

    fmt.Println(sheetName)
    rows, err := f.GetRows(sheetName)
    rlast := len(rows)
    fmt.Println(rows)
    fmt.Println(rlast)

    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
    fmt.Println(data["Name"])
    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)
    }
}

This code works for me.这段代码对我有用。 Lets say input is jim;jam;假设输入是 jim;jam; ==> my o/p is as below in single cell of excel. ==> 我的 o/p 在 excel 的单个单元格中如下所示。 jim jam吉姆果酱

Also, yes executing template is making no sense but i have created it just to see recently submitted data on website.此外,是的,执行模板没有任何意义,但我创建它只是为了查看网站上最近提交的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM