Skip to content

Commit

Permalink
cell text/background color 설정 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
smok95 committed Sep 16, 2022
1 parent 9889965 commit 3d9a58f
Show file tree
Hide file tree
Showing 17 changed files with 786 additions and 619 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 0.2.0 -
### Added
- Cell text/background color 설정 기능 추가

## [0.1.0] - 2020-04-05
### Added
- version 추가
Expand All @@ -15,4 +19,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 날짜가 아닌 형식까지 날짜로 변환되는 현상 수정
- 함수문자열 지원되도록 수정

[0.1.0: https://github.com/smok95/xlsx
[0.1.0: https://github.com/smok95/xlsx]
38 changes: 38 additions & 0 deletions DEV.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 개발환경 구축 방법

- go1.19.1 windows/amd64 [https://go.dev/dl/go1.19.1.windows-amd64.msi]
- tdm64-gcc-10.3.0-2.exe [https://jmeubank.github.io/tdm-gcc/articles/2021-05/10.3.0-release]


## 32bit 빌드

go env 로 아래와 같이 설정되어 있는지확인
cmd.exe에서 할것 (powersehll에서는 적용이 안됨)
set GOARCH=386
set CGO_ENABLED=1
set CC=mingw32-gcc

// 빌드 방법
- Reduce complied file size
-ldflags "-s -w"
* dll
go build -ldflags "-s -w" -buildmode=c-shared -o xlsx.dll main.go
* exe
go build -ldflags "-s -w" -o xlsx.exe main.go


## 64bit 빌드

go env 로 아래와 같이 설정되어 있는지확인

set GOARCH=amd64
set CGO_ENABLED=1
set CC=x86_64-w64-mingw32-gcc

// 빌드 방법
- Reduce complied file size
-ldflags "-s -w"
* dll
go build -ldflags "-s -w" -buildmode=c-shared -o xlsx.dll main.go
* exe
go build -ldflags "-s -w" -o xlsx.exe main.go
12 changes: 12 additions & 0 deletions ansi.sample_style.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
background-color:FF00FF00;color:ff0000ff; background-color:ff0000ff;color:ff00ffff;



color:ff0000ff
color:ff0000ff color:ff0000ff;background-color:ffffaaaa color:ff0000ff;background-color:ffff0000 color:aa000000;background-color:ffff00ff background-color:ffff0000
background-color:aa000000;color:00ffffff color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000
color:ff0000ff color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000
color:ff0000ff color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000
color:ff0000ff color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000
color:ff0000ff color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000
color:ff0000ff color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000 color:ff0000ff;background-color:ffff0000
12 changes: 12 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set GOARCH=386
set CGO_ENABLED=1
set CC=mingw32-gcc
go build -ldflags "-s -w" -buildmode=c-shared -o xlsx.dll main.go
go build -ldflags "-s -w" -o xlsx.exe main.go

set GOARCH=amd64
set CGO_ENABLED=1
set CC=x86_64-w64-mingw32-gcc

go build -ldflags "-s -w" -buildmode=c-shared -o xlsx-x64.dll main.go
go build -ldflags "-s -w" -o xlsx-x64.exe main.go
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/smok95/xlsx

go 1.19

require github.com/tealeg/xlsx v1.0.5
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
30 changes: 11 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"C"

xl "./smok95/xlsx"
xl "github.com/smok95/xlsx/xlsx"
)
import (
"flag"
Expand Down Expand Up @@ -50,32 +50,24 @@ func main() {
}

csv = string(buf)
xl.ConvertFromCSV(csv, opt)
xl.ConvertFromCSV(csv, opt, "")
}

//export csv2xlsx
func csv2xlsx(pszText *C.char, pszOptions *C.char) C.long {
func csv2xlsx(pszCsv *C.char, pszOptions *C.char) C.long {
return csv2xlsx_with_style(pszCsv, pszOptions, nil)
}

//export csv2xlsx_with_style
func csv2xlsx_with_style(pszCsv *C.char, pszOptions *C.char, pszStyles *C.char) C.long {
var opt xl.Options
text := C.GoString(pszText)
text := C.GoString(pszCsv)
cmdline := C.GoString(pszOptions)
styles := C.GoString(pszStyles)
fs := flag.NewFlagSet("csv2xlsxOptions", flag.ContinueOnError)
initFlag(fs, &opt)
args := strings.Split(cmdline, "\n")
fs.Parse(args)
ret := xl.ConvertFromCSV(text, opt)
ret := xl.ConvertFromCSV(text, opt, styles)
return C.long(ret)
}

/*
// 사용한 go version go1.14.1 windows/amd64
go env 로 아래와 같이 설정되어 있는지확인
set GOARCH=386
set CGO_ENABLED=1
// 빌드 방법
- Reduce complied file size
-ldflags "-s -w"
* dll
go build -ldflags "-s -w" -buildmode=c-shared -o xlsx.dll main.go
* exe
go build -ldflags "-s -w" -o xlsx.exe main.go
*/
1 change: 1 addition & 0 deletions test.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xlsx.exe -in utf8.sample.csv -out result.xlsx -delimiter=tab
66 changes: 53 additions & 13 deletions test/xlsx.test/xlsx.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <iostream>
#include <fstream>

typedef int (*fnCsv2xlsx)(const char* pszText, const char* pszOutFile);
typedef int (*csv2xlsx_ptr)(const char* pszCsv, const char* pszOptions);
typedef int (*csv2xlsx_with_style_ptr)(const char* pszCsv, const char* pszOptions, const char* pszStyles);


std::string mbstr2utf8(const char* pszText) {
std::wstring sUnicode = CA2W(pszText).m_psz;
Expand All @@ -30,26 +32,59 @@ std::string readfile(const char* filename) {
return lines;
}

void test() {
void test_csv2xlsx() {
int result = -1;
std::string sAnsiCsv = readfile("ansi.sample.csv");

LPCSTR procName = "csv2xlsx";

if (HMODULE hDll = LoadLibrary("xlsx.dll")) {

csv2xlsx_ptr csv2xlsx = (csv2xlsx_ptr)GetProcAddress(hDll, procName);
if (csv2xlsx) {
std::string sOptions = mbstr2utf8("-out=ansi.sample.xlsx\n-delimiter=\\t\n-sheet-name=ansi sample");
std::string sCsv = mbstr2utf8(sAnsiCsv.c_str());
result = csv2xlsx(sCsv.c_str(), sOptions.c_str());
}
else {
printf("Failed to GetProcAddress('%s')\n", procName);
}

//do not call FreeLibrary function.
// FreeLibrary(hDll);
}
else {
printf("Failed to LoadLibrary('%s')", procName);
}

printf("엑셀파일 생성결과 : %d\n", result);
}

void test_csv2xlsx_with_style() {
int result = -1;
std::string sSrc = readfile("ansi.sample.csv");
std::string sAnsiCsv = readfile("ansi.sample.csv");
std::string sAnsiStyleCsv = readfile("ansi.sample_style.csv");

LPCSTR procName = "csv2xlsx_with_style";

if (HMODULE hDll = LoadLibrary("xlsx.dll")) {

fnCsv2xlsx text2xlsx = (fnCsv2xlsx)GetProcAddress(hDll, "csv2xlsx");
if (text2xlsx) {
std::string sUtf8 = mbstr2utf8("-out=ansi.sample.xlsx\n-delimiter=\\t\n-sheet-name=ansi sample");
std::string sText = mbstr2utf8(sSrc.c_str());
result = text2xlsx(sText.c_str(), sUtf8.c_str());
csv2xlsx_with_style_ptr csv2xlsx_with_style = (csv2xlsx_with_style_ptr)GetProcAddress(hDll, procName);
if (csv2xlsx_with_style) {
std::string sOptions = mbstr2utf8("-out=ansi.sampleWithStyle.xlsx\n-delimiter=\\t\n-sheet-name=ansi sample");
std::string sCsv = mbstr2utf8(sAnsiCsv.c_str());
std::string sStyle = mbstr2utf8(sAnsiStyleCsv.c_str());
result = csv2xlsx_with_style(sCsv.c_str(), sOptions.c_str(), sStyle.c_str());
}
else {
printf("Failed to GetProcAddress('%s')\n", procName);
}
else
puts("Failed to GetProcAddress('csv2xlsx')");

//do not call FreeLibrary function.
// FreeLibrary(hDll);
}
else {
puts("Failed to LoadLibrary('xlsx.dll')");
printf("Failed to LoadLibrary('%s')", procName);
}

printf("엑셀파일 생성결과 : %d\n", result);
Expand All @@ -58,7 +93,12 @@ void test() {
int main()
{
int count = 2;
for(int i=0; i<count; i++)
test();
for (int i = 0; i < count; i++) {
test_csv2xlsx();
}

for (int i = 0; i < count; i++) {
test_csv2xlsx_with_style();
}
return 0;
}
Binary file modified xlsx-x64.dll
Binary file not shown.
Binary file modified xlsx-x64.exe
Binary file not shown.
14 changes: 10 additions & 4 deletions xlsx-x64.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#line 1 "cgo-builtin-export-prolog"

#include <stddef.h> /* for ptrdiff_t below */
#include <stddef.h>

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H
Expand Down Expand Up @@ -40,11 +40,17 @@ typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef size_t GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
#ifdef _MSC_VER
#include <complex.h>
typedef _Fcomplex GoComplex64;
typedef _Dcomplex GoComplex128;
#else
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
#endif

/*
static assertion to make sure the file is being used on architecture
Expand All @@ -68,8 +74,8 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
extern "C" {
#endif


extern long int csv2xlsx(char* p0, char* p1);
extern __declspec(dllexport) long int csv2xlsx(char* pszCsv, char* pszOptions);
extern __declspec(dllexport) long int csv2xlsx_with_style(char* pszCsv, char* pszOptions, char* pszStyles);

#ifdef __cplusplus
}
Expand Down
Binary file modified xlsx.dll
Binary file not shown.
Binary file modified xlsx.exe
Binary file not shown.
14 changes: 10 additions & 4 deletions xlsx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#line 1 "cgo-builtin-export-prolog"

#include <stddef.h> /* for ptrdiff_t below */
#include <stddef.h>

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H
Expand Down Expand Up @@ -40,11 +40,17 @@ typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt32 GoInt;
typedef GoUint32 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef size_t GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
#ifdef _MSC_VER
#include <complex.h>
typedef _Fcomplex GoComplex64;
typedef _Dcomplex GoComplex128;
#else
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
#endif

/*
static assertion to make sure the file is being used on architecture
Expand All @@ -68,8 +74,8 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
extern "C" {
#endif


extern long int csv2xlsx(char* p0, char* p1);
extern __declspec(dllexport) long int csv2xlsx(char* pszCsv, char* pszOptions);
extern __declspec(dllexport) long int csv2xlsx_with_style(char* pszCsv, char* pszOptions, char* pszStyles);

#ifdef __cplusplus
}
Expand Down
Binary file modified xlsx.test.exe
Binary file not shown.
Loading

0 comments on commit 3d9a58f

Please sign in to comment.