Skip to content
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

When Edit excel file,show warning "发现“22.xlsx”中的部分内容有问题。是否让我们尽量尝试恢复? 如果您信任此工作簿的源,请单击“是”。" #1139

Closed
songxk opened this issue Feb 7, 2022 · 20 comments
Labels
confirmed This issue can be reproduced

Comments

@songxk
Copy link

songxk commented Feb 7, 2022

Description
when edit the excel file with excelize , show the warning "发现“22.xlsx”中的部分内容有问题。是否让我们尽量尝试恢复? 如果您信任此工作簿的源,请单击“是"。"

Steps to reproduce the issue:

  1. create a empty excel file,saved as "22.xlsx"

  2. use code edit it .

    f, _ := excelize.OpenFile("/Users/dev/Downloads/22.xlsx")
    f.SetActiveSheet(0)
    sheetName := f.GetSheetName(0)
    fmt.Println(sheetName)
    er := f.SetCellDefault(sheetName, "C4", "test")
    fmt.Println(er)
    f.Save()

  3. open the 22.xlsx file

Describe the results you received:

Describe the results you expected:

Output of go version:

go version go1.16.4 darwin/amd64

Excelize version or commit ID:

excelize 2.4.1

Environment details (OS, Microsoft Excel™ version, physical, etc.):
macOS Big Sur 11.4
Go 1.16.4
excelize 2.4.1
Excel 16.29(19090802)

@xuri
Copy link
Member

xuri commented Feb 7, 2022

Thanks for your issue. Could you provide your input file attachment without confidential info?

@xuri xuri added the needs more info This issue can't reproduce, need more info label Feb 7, 2022
@songxk
Copy link
Author

songxk commented Feb 7, 2022

22.xlsx

@xuri
Copy link
Member

xuri commented Feb 7, 2022

Please using SetCellValue instead of SetCellDefault currently, I'll fix this issue later.

@xuri xuri added confirmed This issue can be reproduced and removed needs more info This issue can't reproduce, need more info labels Feb 7, 2022
@songxk
Copy link
Author

songxk commented Feb 7, 2022

SetCellValue works, thanks

@xuri xuri closed this as completed in 3f8f4f5 Feb 7, 2022
@xuri
Copy link
Member

xuri commented Feb 7, 2022

I have fixed it, please try upgrading to the master branch code, and this patch will be released in the next version.

@zohu
Copy link

zohu commented Aug 23, 2022

I have fixed it, please try upgrading to the master branch code, and this patch will be released in the next version.

SetRow has the same problem. excelize version is v2.6.0.

@xuri
Copy link
Member

xuri commented Aug 23, 2022

@zohu Thanks for your feedback. Can you show us a complete, standalone example program or reproducible demo? or give a try to upgrade to v2.6.1?

@zohu
Copy link

zohu commented Aug 24, 2022

@zohu Thanks for your feedback. Can you show us a complete, standalone example program or reproducible demo? or give a try to upgrade to v2.6.1?

it's fine after the upgrade. thank you.

xuri added a commit to carbin-gun/excelize that referenced this issue Oct 9, 2022
- Add default value on getting `View` property of sheet views
- Add examples and unit test for set sheet views
- Re-order field on sheet view options
- Fix incorrect build-in number format: 42
- Simplify code for the `stylesReader` function
@floki2020
Copy link

floki2020 commented Oct 14, 2022

Hey,guys,I have the same problem.It can open by WPS,but Office doen't work. I upgrade to v2.6.1! I use the StreamWrieter for set values .Just Like

for rowID := 0; rowID < len(data); rowID++ {
		row := make([]interface{}, 4)
		row[0] = data[rowID].StockyardName
		row[1] = data[rowID].InNumber
		row[2] = data[rowID].OutNumber
		row[3] = utils.ConvertToShortString(data[rowID].CreateAt)
		cell, _ := excelize.CoordinatesToCellName(1, rowID+3)
		if err := streamWriter.SetRow(cell, row); err != nil {
			return "", err
		}
	}

Go: 1.17
Excelize:v2.6.1
Microsoft Excel:16.0.156

@zohu
Copy link

zohu commented Oct 14, 2022

Hey,guys,I have the same problem.It can open by WPS,but Office doen't work. I upgrade to v2.6.1! I use the StreamWrieter for set values .Just Like

for rowID := 0; rowID < len(data); rowID++ {
		row := make([]interface{}, 4)
		row[0] = data[rowID].StockyardName
		row[1] = data[rowID].InNumber
		row[2] = data[rowID].OutNumber
		row[3] = utils.ConvertToShortString(data[rowID].CreateAt)
		cell, _ := excelize.CoordinatesToCellName(1, rowID+3)
		if err := streamWriter.SetRow(cell, row); err != nil {
			return "", err
		}
	}

Go: 1.17 Excelize:v2.6.1 Microsoft Excel:16.0.156

Is it possible that you forgot to call streamWriter.Flush() ?

@floki2020
Copy link

Thanks for your answer,and I Flush() it in the end after method setRows.It works in WPS! DONT WORK IN MAC & PC MicroSoft Excel!

	if err := streamWriter.Flush(); err != nil {
		fmt.Println(err)
		return "", err
	}

@xuri
Copy link
Member

xuri commented Oct 17, 2022

Hi @floki2020, thanks for your feedback. Can you show us a complete, standalone example program or reproducible demo?

@xuri
Copy link
Member

xuri commented Oct 17, 2022

Could you provide a simple and executable reproducible code? There is no definition of the valid data and utils, please reduce the code scope and simplify the code first.

@floki2020
Copy link

floki2020 commented Oct 17, 2022

Could you provide a simple and executable reproducible code? There is no definition of the valid data and utils, please reduce the code scope and simplify the code first.

func BuilderExcel() (string, error) {
	file := excelize.NewFile()
	streamWriter, err := file.NewStreamWriter("Sheet1")
	if err != nil {
		fmt.Println(err)
		return "", err
	}
	sHead, _ := file.NewStyle(&excelize.Style{Alignment: &excelize.Alignment{Horizontal: "Center"}, Font: &excelize.Font{Bold: true}})
	sRow, err := file.NewStyle(&excelize.Style{Alignment: &excelize.Alignment{Horizontal: "Center"}})
	if err != nil {
		fmt.Println(err)
		return "", err
	}

	rowTotal := "A9"

	numStr := "8"

	subStart := "A" + numStr
	subEnd := "D" + numStr

	subHead := "A" + numStr

	err = streamWriter.MergeCell("A1", "D1")
	if err != nil {
		fmt.Print(err)
	}

	streamWriter.MergeCell(subStart, subEnd)

	streamWriter.SetColWidth(1, 4, 20)

	if err := streamWriter.SetRow("A1", []interface{}{
		excelize.Cell{StyleID: sHead, Value: "123"}}); err != nil {

		return "", err
	}
	if err := streamWriter.SetRow(subHead, []interface{}{
		excelize.Cell{StyleID: sHead, Value: "综合统计"}}); err != nil {

		return "", err
	}
	if err := streamWriter.SetRow("A2", []interface{}{
		excelize.Cell{StyleID: sRow, Value: "所属矿场"}, excelize.Cell{StyleID: sRow, Value: "进口车次"}, excelize.Cell{StyleID: sRow, Value: "出口车次"}, excelize.Cell{StyleID: sRow, Value: "日期"}}); err != nil {

		return "", err
	}
	if err := streamWriter.SetRow(rowTotal, []interface{}{
		excelize.Cell{StyleID: sRow, Value: "所属"}, excelize.Cell{StyleID: sRow, Value: "进口车次"}, excelize.Cell{StyleID: sRow, Value: "出口车次"}, excelize.Cell{StyleID: sRow, Value: "日期"}}); err != nil {
		return "", err
	}

	for rowID := 0; rowID < 4; rowID++ {
		row := make([]interface{}, 2)
		row[0] = rowID * 2
		row[1] = rowID + 1

		cell, _ := excelize.CoordinatesToCellName(1, rowID+3)
		if err := streamWriter.SetRow(cell, row); err != nil {
			fmt.Println(err)
			return "", err
		}
	}

	for i := 0; i < 6; i++ {
		row := make([]interface{}, 2)
		row[0] = i * 3
		row[1] = i + 1
		cell, _ := excelize.CoordinatesToCellName(1, i+6)
		if err := streamWriter.SetRow(cell, row); err != nil {
			fmt.Println(err)
			return "", err
		}
	}
	if err := streamWriter.Flush(); err != nil {
		fmt.Println(err)
		return "", err
	}

	file.NewSheet("Sheet2")
	truckStm, err := file.NewStreamWriter("Sheet2")
	if err != nil {
		fmt.Println(err)
		return "", err
	}
	truckStm.SetColWidth(1, 4, 20)
	if err := truckStm.SetRow("A1", []interface{}{
		excelize.Cell{StyleID: sRow, Value: "所属矿场"}, excelize.Cell{StyleID: sRow, Value: "车牌号"}, excelize.Cell{StyleID: sRow, Value: "位置"}, excelize.Cell{StyleID: sRow, Value: "日期"}}); err != nil {
		fmt.Println(err)
		return "", err
	}
	for idx := 0; idx < 5; idx++ {
		row := make([]interface{}, 2)
		row[0] = "123"
		row[1] = idx * 2
		cell, _ := excelize.CoordinatesToCellName(1, idx+2)
		if err := truckStm.SetRow(cell, row); err != nil {
			fmt.Println(err)
			return "", err
		}
	}
	if err := truckStm.Flush(); err != nil {
		fmt.Println(err)
		return "", err
	}
	path := "report/" + "test" + ".xlsx"
	if err := file.SaveAs(path); err != nil {
		fmt.Println(err)
		return "", err
	}
	return path, nil
}

@floki2020
Copy link

I think maybe method MergeCell problem,when i remove it from the code,it works in excel

@xuri
Copy link
Member

xuri commented Oct 17, 2022

As the documentation of the NewStreamWriter function says: "ensure that the order of row numbers is ascending".

@floki2020
Copy link

As the documentation of the NewStreamWriter function says: "ensure that the order of row numbers is ascending".

My oder line number is ascending. I don't think it's wrong :). Written according to the demo,i just use the funciton MergeCell

@xuri
Copy link
Member

xuri commented Oct 18, 2022

The order of row numbers is not ascending in your code. The variable subHead was A8, which is in row 8, so you need to set this row after the second row, for example, using:

if err := streamWriter.SetRow("A2", []interface{}{
    excelize.Cell{StyleID: sRow, Value: "所属矿场"}, excelize.Cell{StyleID: sRow, Value: "进口车次"}, excelize.Cell{StyleID: sRow, Value: "出口车次"}, excelize.Cell{StyleID: sRow, Value: "日期"}}); err != nil {
    return "", err
}
if err := streamWriter.SetRow(subHead, []interface{}{
    excelize.Cell{StyleID: sHead, Value: "综合统计"}}); err != nil {
    return "", err
}

instead of

if err := streamWriter.SetRow(subHead, []interface{}{
    excelize.Cell{StyleID: sHead, Value: "综合统计"}}); err != nil {

    return "", err
}
if err := streamWriter.SetRow("A2", []interface{}{
    excelize.Cell{StyleID: sRow, Value: "所属矿场"}, excelize.Cell{StyleID: sRow, Value: "进口车次"}, excelize.Cell{StyleID: sRow, Value: "出口车次"}, excelize.Cell{StyleID: sRow, Value: "日期"}}); err != nil {

    return "", err
}

Please also check the row number in the for loop statements. I'll consider checking the row number in the stream writer and returning the error to avoid this issue.

@floki2020
Copy link

The order of row numbers is not ascending in your code. The variable subHead was A8, which is in row 8, so you need to set this row after the second row, for example, using:

if err := streamWriter.SetRow("A2", []interface{}{
    excelize.Cell{StyleID: sRow, Value: "所属矿场"}, excelize.Cell{StyleID: sRow, Value: "进口车次"}, excelize.Cell{StyleID: sRow, Value: "出口车次"}, excelize.Cell{StyleID: sRow, Value: "日期"}}); err != nil {
    return "", err
}
if err := streamWriter.SetRow(subHead, []interface{}{
    excelize.Cell{StyleID: sHead, Value: "综合统计"}}); err != nil {
    return "", err
}

instead of

if err := streamWriter.SetRow(subHead, []interface{}{
    excelize.Cell{StyleID: sHead, Value: "综合统计"}}); err != nil {

    return "", err
}
if err := streamWriter.SetRow("A2", []interface{}{
    excelize.Cell{StyleID: sRow, Value: "所属矿场"}, excelize.Cell{StyleID: sRow, Value: "进口车次"}, excelize.Cell{StyleID: sRow, Value: "出口车次"}, excelize.Cell{StyleID: sRow, Value: "日期"}}); err != nil {

    return "", err
}

Please also check the row number in the for loop statements. I'll consider checking the row number in the stream writer and returning the error to avoid this issue.

I'm so sorry. I forget this scope.Maybe it can works~ Thanks again

xuri added a commit that referenced this issue Oct 19, 2022
…e result

- An error will be returned when setting the stream row without ascending row numbers, to avoid potential mistakes as mentioned in #1139
- Updated unit tests
@xuri
Copy link
Member

xuri commented Oct 25, 2022

I have added a check of the row number in the stream writer and returned the error: row %d has already been written to avoid row numbers is not ascending mistake. This feature will be released in the next version.

xuri added a commit to JDavidVR/excelize that referenced this issue Jul 11, 2023
…ply the result

- An error will be returned when setting the stream row without ascending row numbers, to avoid potential mistakes as mentioned in qax-os#1139
- Updated unit tests
jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
- Add default value on getting `View` property of sheet views
- Add examples and unit test for set sheet views
- Re-order field on sheet view options
- Fix incorrect build-in number format: 42
- Simplify code for the `stylesReader` function
jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
…ply the result

- An error will be returned when setting the stream row without ascending row numbers, to avoid potential mistakes as mentioned in qax-os#1139
- Updated unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed This issue can be reproduced
Projects
No open projects
Status: Bugfix
Development

No branches or pull requests

4 participants