Giter Club home page Giter Club logo

Comments (9)

xuri avatar xuri commented on May 22, 2024

Thanks for your issue. Which version of the Excelize library are you using? Could you follow the issue template and show us a complete, standalone example program or reproducible demo?

from excelize.

ccaiy9 avatar ccaiy9 commented on May 22, 2024

Thanks for your reply!

my steps to reproduce the issue:

  1. i have a producer process, read data from mysql, and transferring data to consumer processes through pipelines
  2. consumer processes receive data and and export it to xlsx,demo code:
func generateExcelColumn(n int) string {
	result := ""
	for n > 0 {
		remainder := (n - 1) % 26
		result = fmt.Sprintf("%c%s", 'A'+remainder, result)
		n = (n - 1) / 26
	}
	return result
}

func generateExcelCell(column, row int) string {
	columnName := generateExcelColumn(column)
	cell := fmt.Sprintf("%s%d", columnName, row)
	return cell
}


func export() {

	file := excelize.NewFile()
	_, err = file.NewSheet("Sheet1")
	reader := bufio.NewReader(os.Stdin)   // from producer process

	for {
		line, err = reader.ReadString('\n')  // read a line
		line = strings.TrimSuffix(line, "\n") // Remove line breaks at the end of a line
		fields := strings.Split(line, "\t")   // Separate fields by tab
		
		for col, field := range fields {
			cell := generateExcelCell(col+1, row)
			transField := strings.Replace(field, "\\n", "\n", -1)
			
			if err = file.SetCellValue("Sheet1", cell, transField); err != nil {
				os.Exit(-1)
			}
		}
		
		row++

	}
}

the results you received:
When I opened it using WPS and found that a space would be added before the first data in the first column.
Also I get an error: strconv.ParseInt: parsing "\ufeff1": invalid syntax, "\ ufeff1" seems to represent a space.
By the way, only the data in the first row and first column will be added with this space, and other data will not be added.

Describe the results you expected:
I hope that when exporting xlsx, this space will not be added by default

Output of go version:
GOVERSION="go1.19.13"

Excelize version:
"github.com/xuri/excelize/v2"
image

from excelize.

xuri avatar xuri commented on May 22, 2024

Thanks for your feedback. This duplicate with issue #1778, As the documentation of the SetCellValue function says, it supports multiple data types. Please convert the transField variable to Go's numeric data type (int, uint, float64, etc.) in your code before setting the cell value.

from excelize.

ccaiy9 avatar ccaiy9 commented on May 22, 2024

My issue may be different from #1778,non numeric data can also have this situation:
image

from excelize.

xuri avatar xuri commented on May 22, 2024

Could you follow the issue template and show us a complete, standalone example program or reproducible demo (with a main function)? Please check and confirm that the value of the variable transField doesn't contain a blank character prefix.

from excelize.

ccaiy9 avatar ccaiy9 commented on May 22, 2024

I have a simple test:

1. mysql data
image
image

2.export result:
image

3. I added a few lines for printing:

func generateExcelColumn(n int) string {
	result := ""
	for n > 0 {
		remainder := (n - 1) % 26
		result = fmt.Sprintf("%c%s", 'A'+remainder, result)
		n = (n - 1) / 26
	}
	return result
}

func generateExcelCell(column, row int) string {
	columnName := generateExcelColumn(column)
	cell := fmt.Sprintf("%s%d", columnName, row)
	return cell
}


func export() {

	file := excelize.NewFile()
	_, err = file.NewSheet("Sheet1")
	reader := bufio.NewReader(os.Stdin)   // from producer process

	for {
		line, err = reader.ReadString('\n')  // read a line
		
		logger.Infof(ctx, "-1--------------line:%s", line)
		
		line = strings.TrimSuffix(line, "\n") // Remove line breaks at the end of a line
		fields := strings.Split(line, "\t")   // Separate fields by tab
		
		for col, field := range fields {
			
			logger.Infof(ctx, "-2--------------field:%s", field)
			
			cell := generateExcelCell(col+1, row)
			transField := strings.Replace(field, "\\n", "\n", -1)
			
			logger.Infof(ctx, "-3--------------transField:%s", transField)
			
			if err = file.SetCellValue("Sheet1", cell, transField); err != nil {
				os.Exit(-1)
			}
		}
		
		row++

	}
}

Print results:

2024-02-04 18:39:37.069 [INFO] {c52f1bd66cc19d05628bd8bf27af3ad6} -1--------------line:Quién4   1
2024-02-04 18:39:37.069 [INFO] {c52f1bd66cc19d05628bd8bf27af3ad6} -2--------------field:Quién4
2024-02-04 18:39:37.069 [INFO] {c52f1bd66cc19d05628bd8bf27af3ad6} -3--------------transField:Quién4

From the print results, it can be seen that from line, err=reader ReadString ('\ n') reads data without spaces, and the value of the variable transField doesn't contain a blank character prefix.
So, I guess, it looks like I missed some settings when using this package?

from excelize.

xuri avatar xuri commented on May 22, 2024

Could you remove the database and context-related code to simplify the code, and show us a complete, standalone example program? The demo should be with a main function to reproducible this problem by executing directly.

from excelize.

ccaiy9 avatar ccaiy9 commented on May 22, 2024

Seems to have found the problem.
i print with %q:

2024-02-04 19:55:58.175 [INFO] {c52f1bd66cc19d05628bd8bf27af3ad6} -1--------------line:"\ufeffQuien4\t1\n"

Quien4 has a special character before it....
emm, thanks for your help,
It seems like to be caused by the use of stdout or stdin....

from excelize.

xuri avatar xuri commented on May 22, 2024

You're welcome. Please close the issue if this problem has been resolved.

from excelize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.