簡體   English   中英

試圖提交 html 表單數據到 excel

[英]Trying to submit html form data to excel

我成功編寫代碼以將 html 表單數據作為單個 excel 行提交。 但是在我的 html 表單中,我想提交多個數據,這些數據應該附加在 excel 的單個單元格中,但數據的第二部分應該以同一單元格中的新行開頭。 就像(ALT + Enter 操作)。 我的 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 jam 都在單個單元格中,但在 excel 中與新行卡住。

如果我正確閱讀了您的查詢,我相信您在問如何將新的文本行添加到 excel 單元格中(您可以在 Excel 應用程序中使用 ALT+Enter 來執行此操作)。 數據的來源似乎與您的問題無關。

在 Excel(在 Windows 上)中,一個單元格中的多行由換行符(char 0xA 或\n in go)分割。 但是,當您添加換行符時,Excel 會自動將單元格設置為“換行”(如果您不這樣做,則換行符存在但不顯示),這有一點復雜。

這是如何在 Go 中實現的示例; 每次運行應用程序時,它都會在單元格 A1 中添加一個新行。 請注意,我沒有更改單元格高度,因此您需要這樣做以使額外的行可見:

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)
    }
}

感謝您的反饋和時間來解決我的問題。

最后,我在下面的代碼的幫助下實現了我的目標。 它有點復雜,但它對我有用,我希望它對其他人也有幫助。


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)
    }
}

這段代碼對我有用。 假設輸入是 jim;jam; ==> 我的 o/p 在 excel 的單個單元格中如下所示。 吉姆果醬

此外,是的,執行模板沒有任何意義,但我創建它只是為了查看網站上最近提交的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM