Quantcast
Channel: NPOI
Viewing all 1621 articles
Browse latest View live

Created Unassigned: The style setting problem for xls file [13150]

$
0
0
"OES Ch Input data file.xls" This file is the template Excel
"123.xls" This file is modified by NPOI

The following is the code:

```
public static MemoryStream Export(string tempFilePath)
{
IWorkbook iworkbook = null;
HSSFWorkbook hwb = null;
using (FileStream file = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
{
hwb = new HSSFWorkbook(file);
iworkbook = hwb;
}

ICellStyle textWhiteStyle = getWhiteText(hwb);

ISheet sheet1 = iworkbook.GetSheet("OES IAM Raw data");

String dataSql = @"Select
DPP.SN,
DPR.PartNumber,
DPR.PartDescription_EN,
DPR.PartDescription_CN,
DPR.VehicleModel_EN,
DPR.Brand,
MICT.TypeName_EN,
MI.ID as MIID,
MIC.CorporationName_EN,
MPMF.PartsBrand_EN,
MPQ.QualityLongName_EN,
MPMF.ID as MPMFID,
SIL.Time,
DPP.MSRPWZVAT,
DPP.DNPWZVAT,
DPP.Remark
From Data_PartPrice DPP
Left Join Data_PartRequire DPR On DPP.SN = DPR.SN
Left Join Sys_ImportLog SIL On DPP.Informants_CN = SIL.Informants_CN
Left Join Master_IAMInformants MI On SIL.Informants_CN = MI.Informants_CN
Left Join Master_IAMCorpList MIC On Mi.CorporationName_CN = MIC.CorporationName_CN
Left Join Master_IAMCorpType MICT On MIC.TypeName_CN = MICT.TypeName_CN
Left Join Master_PartsManufacturers MPMF On DPP.PartsBrand_CN = MPMF.PartsBrand_CN
Left Join Master_PartsQuality MPQ On DPP.QualityType = MPQ.QualityType
Where SIL.City = '{0}'
Order By DPP.SN";
string citySql = @"Select distinct MC.City_EN,MC.City_CN From Data_PartPrice DPP Left Join Sys_ImportLog SIL On DPP.Informants_CN = SIL.Informants_CN
Left Join Master_City MC On SIL.City = MC.City_CN Where MC.City_EN is not null Order By MC.City_EN";

DataTable cityTable = DbHelperSQL.Query(citySql).Tables[0];

for (int i = 0; i < cityTable.Rows.Count; i++)
{
string cityName = cityTable.Rows[i][0].ToString();
ISheet saveSheet = iworkbook.CloneSheet(1);
iworkbook.SetSheetName(iworkbook.GetSheetIndex(saveSheet), "OES IAM Raw data - " + cityName.ToString());
string useSql = String.Format(dataSql, cityTable.Rows[i][1]);
DataTable dataTable = DbHelperSQL.Query(useSql).Tables[0];
string sn = String.Empty;
int lastUserSN = 0;
int fromIndex = 4;
foreach (DataRow dr in dataTable.Rows)
{
IRow row = saveSheet.CreateRow(fromIndex);

ICell cell = row.CreateCell(1);
if (dr["SN"].ToString() != sn)
{
sn = dr["SN"].ToString();
cell.SetCellValue(sn);
lastUserSN = Convert.ToInt32(sn);
}
else
{
lastUserSN = lastUserSN + 10000;
cell.SetCellValue(lastUserSN.ToString());
}
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(2);
cell.SetCellValue("OES/IAM");
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(3);
cell.SetCellValue("China - " + cityName);
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(4);
cell.SetCellValue(dr["PartNumber"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(5);
cell.SetCellValue(dr["PartNumber"].ToString().Replace(" ","").Replace("-",""));
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(6);
cell.SetCellValue(dr["PartDescription_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(7);
cell.SetCellValue(dr["PartDescription_CN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(8);
cell.SetCellValue(dr["VehicleModel_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(9);
cell.SetCellValue(dr["Brand"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(10);
cell.SetCellValue(dr["TypeName_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(11);
cell.SetCellValue(dr["MIID"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(12);
cell.SetCellValue(dr["CorporationName_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(13);
cell.SetCellValue(dr["PartsBrand_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(14);
cell.SetCellValue(dr["QualityLongName_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(15);
cell.SetCellValue(dr["MPMFID"].ToString());
//cell.CellStyle = textWhiteStyle;

//cell = row.CreateCell(16);
//cell.SetCellValue(dr["MPMFID"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(16);
cell.SetCellValue(Convert.ToDateTime(dr["Time"]).ToString("yyyy/MM/dd"));
//cell.CellStyle = textWhiteStyle;

//17列 不知道放什么 R

//18列 固定字符
cell = row.CreateCell(18);
cell.SetCellValue("CNY");
//cell.CellStyle = textWhiteStyle;

//19列 不知道放什么

cell = row.CreateCell(20);
cell.SetCellValue(dr["MSRPWZVAT"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(21);
cell.SetCellValue(dr["DNPWZVAT"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(22);
cell.CellFormula = String.Format("U{0}/R{1}", fromIndex + 1, fromIndex + 1);
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(23);
cell.CellFormula = String.Format("V{0}/R{1}", fromIndex + 1, fromIndex + 1);
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(24);
cell.SetCellValue(dr["Remark"].ToString());
//cell.CellStyle = textWhiteStyle;

fromIndex++;
}
}

iworkbook.RemoveSheetAt(1);

MemoryStream memFile = new MemoryStream();
iworkbook.Write(memFile);

FileStream file1 = new FileStream(@"C:/123.xls", FileMode.Create);
iworkbook.Write(file1);
file1.Close();

//C1.C1Excel.C1XLBook book = new C1.C1Excel.C1XLBook();
//book.Load(@"C:/123.xls", C1.C1Excel.FileFormat.OpaqueBiff8, true);
//book.Sheets[0].GetCell(0, 0).Style.BorderColorBottom = Color.Black;
//book.Save(@"C:/123.xls", C1.C1Excel.FileFormat.OpaqueBiff8);


return memFile;
}

private static ICellStyle getWhiteText(HSSFWorkbook hwb)
{
IFont font1 = hwb.CreateFont();
font1.FontHeightInPoints = 9;
font1.FontName = "Arial Narrow";

ICellStyle style2 = hwb.CreateCellStyle();
style2.SetFont(font1);
style2.BorderBottom = BorderStyle.Thin;
style2.BorderLeft = BorderStyle.Thin;
style2.BorderRight = BorderStyle.Thin;
style2.BorderTop = BorderStyle.Thin;
style2.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.Alignment = HorizontalAlignment.Left;
style2.WrapText = true;

return style2;
}
```
I found that if it is set style by the NOPI DLL, the template Excel style will be lost. If I do not implement the "getWhiteText" function, it will not be lost.

Please help me! thanks!

Commented Unassigned: The style setting problem for xls file [13150]

$
0
0
"OES Ch Input data file.xls" This file is the template Excel
"123.xls" This file is modified by NPOI

The following is the code:

```
public static MemoryStream Export(string tempFilePath)
{
IWorkbook iworkbook = null;
HSSFWorkbook hwb = null;
using (FileStream file = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
{
hwb = new HSSFWorkbook(file);
iworkbook = hwb;
}

ICellStyle textWhiteStyle = getWhiteText(hwb);

ISheet sheet1 = iworkbook.GetSheet("OES IAM Raw data");

String dataSql = @"Select
DPP.SN,
DPR.PartNumber,
DPR.PartDescription_EN,
DPR.PartDescription_CN,
DPR.VehicleModel_EN,
DPR.Brand,
MICT.TypeName_EN,
MI.ID as MIID,
MIC.CorporationName_EN,
MPMF.PartsBrand_EN,
MPQ.QualityLongName_EN,
MPMF.ID as MPMFID,
SIL.Time,
DPP.MSRPWZVAT,
DPP.DNPWZVAT,
DPP.Remark
From Data_PartPrice DPP
Left Join Data_PartRequire DPR On DPP.SN = DPR.SN
Left Join Sys_ImportLog SIL On DPP.Informants_CN = SIL.Informants_CN
Left Join Master_IAMInformants MI On SIL.Informants_CN = MI.Informants_CN
Left Join Master_IAMCorpList MIC On Mi.CorporationName_CN = MIC.CorporationName_CN
Left Join Master_IAMCorpType MICT On MIC.TypeName_CN = MICT.TypeName_CN
Left Join Master_PartsManufacturers MPMF On DPP.PartsBrand_CN = MPMF.PartsBrand_CN
Left Join Master_PartsQuality MPQ On DPP.QualityType = MPQ.QualityType
Where SIL.City = '{0}'
Order By DPP.SN";
string citySql = @"Select distinct MC.City_EN,MC.City_CN From Data_PartPrice DPP Left Join Sys_ImportLog SIL On DPP.Informants_CN = SIL.Informants_CN
Left Join Master_City MC On SIL.City = MC.City_CN Where MC.City_EN is not null Order By MC.City_EN";

DataTable cityTable = DbHelperSQL.Query(citySql).Tables[0];

for (int i = 0; i < cityTable.Rows.Count; i++)
{
string cityName = cityTable.Rows[i][0].ToString();
ISheet saveSheet = iworkbook.CloneSheet(1);
iworkbook.SetSheetName(iworkbook.GetSheetIndex(saveSheet), "OES IAM Raw data - " + cityName.ToString());
string useSql = String.Format(dataSql, cityTable.Rows[i][1]);
DataTable dataTable = DbHelperSQL.Query(useSql).Tables[0];
string sn = String.Empty;
int lastUserSN = 0;
int fromIndex = 4;
foreach (DataRow dr in dataTable.Rows)
{
IRow row = saveSheet.CreateRow(fromIndex);

ICell cell = row.CreateCell(1);
if (dr["SN"].ToString() != sn)
{
sn = dr["SN"].ToString();
cell.SetCellValue(sn);
lastUserSN = Convert.ToInt32(sn);
}
else
{
lastUserSN = lastUserSN + 10000;
cell.SetCellValue(lastUserSN.ToString());
}
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(2);
cell.SetCellValue("OES/IAM");
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(3);
cell.SetCellValue("China - " + cityName);
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(4);
cell.SetCellValue(dr["PartNumber"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(5);
cell.SetCellValue(dr["PartNumber"].ToString().Replace(" ","").Replace("-",""));
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(6);
cell.SetCellValue(dr["PartDescription_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(7);
cell.SetCellValue(dr["PartDescription_CN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(8);
cell.SetCellValue(dr["VehicleModel_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(9);
cell.SetCellValue(dr["Brand"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(10);
cell.SetCellValue(dr["TypeName_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(11);
cell.SetCellValue(dr["MIID"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(12);
cell.SetCellValue(dr["CorporationName_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(13);
cell.SetCellValue(dr["PartsBrand_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(14);
cell.SetCellValue(dr["QualityLongName_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(15);
cell.SetCellValue(dr["MPMFID"].ToString());
//cell.CellStyle = textWhiteStyle;

//cell = row.CreateCell(16);
//cell.SetCellValue(dr["MPMFID"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(16);
cell.SetCellValue(Convert.ToDateTime(dr["Time"]).ToString("yyyy/MM/dd"));
//cell.CellStyle = textWhiteStyle;

//17列 不知道放什么 R

//18列 固定字符
cell = row.CreateCell(18);
cell.SetCellValue("CNY");
//cell.CellStyle = textWhiteStyle;

//19列 不知道放什么

cell = row.CreateCell(20);
cell.SetCellValue(dr["MSRPWZVAT"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(21);
cell.SetCellValue(dr["DNPWZVAT"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(22);
cell.CellFormula = String.Format("U{0}/R{1}", fromIndex + 1, fromIndex + 1);
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(23);
cell.CellFormula = String.Format("V{0}/R{1}", fromIndex + 1, fromIndex + 1);
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(24);
cell.SetCellValue(dr["Remark"].ToString());
//cell.CellStyle = textWhiteStyle;

fromIndex++;
}
}

iworkbook.RemoveSheetAt(1);

MemoryStream memFile = new MemoryStream();
iworkbook.Write(memFile);

FileStream file1 = new FileStream(@"C:/123.xls", FileMode.Create);
iworkbook.Write(file1);
file1.Close();

//C1.C1Excel.C1XLBook book = new C1.C1Excel.C1XLBook();
//book.Load(@"C:/123.xls", C1.C1Excel.FileFormat.OpaqueBiff8, true);
//book.Sheets[0].GetCell(0, 0).Style.BorderColorBottom = Color.Black;
//book.Save(@"C:/123.xls", C1.C1Excel.FileFormat.OpaqueBiff8);


return memFile;
}

private static ICellStyle getWhiteText(HSSFWorkbook hwb)
{
IFont font1 = hwb.CreateFont();
font1.FontHeightInPoints = 9;
font1.FontName = "Arial Narrow";

ICellStyle style2 = hwb.CreateCellStyle();
style2.SetFont(font1);
style2.BorderBottom = BorderStyle.Thin;
style2.BorderLeft = BorderStyle.Thin;
style2.BorderRight = BorderStyle.Thin;
style2.BorderTop = BorderStyle.Thin;
style2.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.Alignment = HorizontalAlignment.Left;
style2.WrapText = true;

return style2;
}
```
I found that if it is set style by the NOPI DLL, the template Excel style will be lost. If I do not implement the "getWhiteText" function, it will not be lost.

Please help me! thanks!
Comments: ** Comment from web user: 87384094 **

123.xls file

New Post: Equivalent of XSSFImportFromXML ion NPOI

$
0
0
XSSFImportFromXML seems to be available in POI but not available in NPOI.. can someone tell me how i can access it? I mean what references do i need to use in .NET?

Created Unassigned: xlsx format file with xls extension issue [13153]

$
0
0
hi i have an xslx format file but the user will upload it with an . xls extension.

IN my code im checking the extension...
if its .xlsx im using XssfWorkboox if its .xls im using HssfWorkbook...
BUt its giving me an error .. "Invalid header".. pls help me with a solution...
user cannot change the extension...


if (fileExt.ToLower() == ".xls")
{
isXlsxXSSFfFile = false;
//Use the NPOI Excel xls object
using (FileStream file = new FileStream(_pathAndFile, FileMode.Open, FileAccess.Read))
{
hssfwb = new HSSFWorkbook(file);
}
if (string.IsNullOrEmpty(_sheetName))
{
hssfSheet = (HSSFSheet)hssfwb.GetSheetAt(0);
}
else
{
if (this.SheetExists(_sheetName))
hssfSheet = (HSSFSheet)hssfwb.GetSheet(_sheetName);
else
throw new ObjectNotFoundException("Sheet name doesn't exist");
}
}
else //.xlsx extension
{
isXlsxXSSFfFile = true;

//Use the NPOI Excel xlsx object
using (FileStream file = new FileStream(_pathAndFile, FileMode.Open, FileAccess.Read))
{
xssfwb = new XSSFWorkbook(file);
}
if (string.IsNullOrEmpty(_sheetName))
{
xssfSheet = (XSSFSheet)xssfwb.GetSheetAt(0);

}
else
{
if (this.SheetExists(_sheetName))
xssfSheet = (XSSFSheet)xssfwb.GetSheet(_sheetName);
else
throw new ObjectNotFoundException("Sheet name doesn't exist");
}

}

Commented Unassigned: xlsx format file with xls extension issue [13153]

$
0
0
hi i have an xslx format file but the user will upload it with an . xls extension.

IN my code im checking the extension...
if its .xlsx im using XssfWorkboox if its .xls im using HssfWorkbook...
BUt its giving me an error .. "Invalid header".. pls help me with a solution...
user cannot change the extension...


if (fileExt.ToLower() == ".xls")
{
isXlsxXSSFfFile = false;
//Use the NPOI Excel xls object
using (FileStream file = new FileStream(_pathAndFile, FileMode.Open, FileAccess.Read))
{
hssfwb = new HSSFWorkbook(file);
}
if (string.IsNullOrEmpty(_sheetName))
{
hssfSheet = (HSSFSheet)hssfwb.GetSheetAt(0);
}
else
{
if (this.SheetExists(_sheetName))
hssfSheet = (HSSFSheet)hssfwb.GetSheet(_sheetName);
else
throw new ObjectNotFoundException("Sheet name doesn't exist");
}
}
else //.xlsx extension
{
isXlsxXSSFfFile = true;

//Use the NPOI Excel xlsx object
using (FileStream file = new FileStream(_pathAndFile, FileMode.Open, FileAccess.Read))
{
xssfwb = new XSSFWorkbook(file);
}
if (string.IsNullOrEmpty(_sheetName))
{
xssfSheet = (XSSFSheet)xssfwb.GetSheetAt(0);

}
else
{
if (this.SheetExists(_sheetName))
xssfSheet = (XSSFSheet)xssfwb.GetSheet(_sheetName);
else
throw new ObjectNotFoundException("Sheet name doesn't exist");
}

}
Comments: ** Comment from web user: tonyqus **

If you have some budget on this, I can provide you a solution. For details, please contact support@neuzilla.com

Edited Issue: SetSheetOrder breaks XLSX [13142]

$
0
0
NPOI 2.1
XSSFWorkbook.cs

Issue:
When setting the sheet order (i.e. moving a worksheet) with XSSF, the XLSX file will break and cannot be opened with Excel.

Reason:
CT_Sheet.Set will set the SheetId of the moved sheet to 0, which the OpenXML format does not allow.

our workaround:
keep the sheetId
```
public void SetSheetOrder(String sheetname, int pos)
{
int idx = GetSheetIndex(sheetname);
XSSFSheet sheet = sheets[idx];
sheets.RemoveAt(idx);
sheets.Insert(pos,sheet);
// Reorder CT_Sheets
CT_Sheets ct = workbook.sheets;
CT_Sheet cts = ct.GetSheetArray(idx).Copy();

// get sheetid from sheet to be moved
uint Mysheetid = ct.GetSheetArray(idx).sheetId;

workbook.sheets.RemoveSheet(idx);
CT_Sheet newcts = ct.InsertNewSheet(pos);
newcts.Set(cts);

// retrieve sheetid
newcts.sheetId = Mysheetid;

//notify sheets
for (int i = 0; i < sheets.Count; i++)
{
sheets[i].sheet = ct.GetSheetArray(i);
}
}
```

New Post: Equivalent of XSSFImportFromXML ion NPOI

$
0
0
This is never implemented in NPOI before.

Commented Unassigned: The style setting problem for xls file [13150]

$
0
0
"OES Ch Input data file.xls" This file is the template Excel
"123.xls" This file is modified by NPOI

The following is the code:

```
public static MemoryStream Export(string tempFilePath)
{
IWorkbook iworkbook = null;
HSSFWorkbook hwb = null;
using (FileStream file = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
{
hwb = new HSSFWorkbook(file);
iworkbook = hwb;
}

ICellStyle textWhiteStyle = getWhiteText(hwb);

ISheet sheet1 = iworkbook.GetSheet("OES IAM Raw data");

String dataSql = @"Select
DPP.SN,
DPR.PartNumber,
DPR.PartDescription_EN,
DPR.PartDescription_CN,
DPR.VehicleModel_EN,
DPR.Brand,
MICT.TypeName_EN,
MI.ID as MIID,
MIC.CorporationName_EN,
MPMF.PartsBrand_EN,
MPQ.QualityLongName_EN,
MPMF.ID as MPMFID,
SIL.Time,
DPP.MSRPWZVAT,
DPP.DNPWZVAT,
DPP.Remark
From Data_PartPrice DPP
Left Join Data_PartRequire DPR On DPP.SN = DPR.SN
Left Join Sys_ImportLog SIL On DPP.Informants_CN = SIL.Informants_CN
Left Join Master_IAMInformants MI On SIL.Informants_CN = MI.Informants_CN
Left Join Master_IAMCorpList MIC On Mi.CorporationName_CN = MIC.CorporationName_CN
Left Join Master_IAMCorpType MICT On MIC.TypeName_CN = MICT.TypeName_CN
Left Join Master_PartsManufacturers MPMF On DPP.PartsBrand_CN = MPMF.PartsBrand_CN
Left Join Master_PartsQuality MPQ On DPP.QualityType = MPQ.QualityType
Where SIL.City = '{0}'
Order By DPP.SN";
string citySql = @"Select distinct MC.City_EN,MC.City_CN From Data_PartPrice DPP Left Join Sys_ImportLog SIL On DPP.Informants_CN = SIL.Informants_CN
Left Join Master_City MC On SIL.City = MC.City_CN Where MC.City_EN is not null Order By MC.City_EN";

DataTable cityTable = DbHelperSQL.Query(citySql).Tables[0];

for (int i = 0; i < cityTable.Rows.Count; i++)
{
string cityName = cityTable.Rows[i][0].ToString();
ISheet saveSheet = iworkbook.CloneSheet(1);
iworkbook.SetSheetName(iworkbook.GetSheetIndex(saveSheet), "OES IAM Raw data - " + cityName.ToString());
string useSql = String.Format(dataSql, cityTable.Rows[i][1]);
DataTable dataTable = DbHelperSQL.Query(useSql).Tables[0];
string sn = String.Empty;
int lastUserSN = 0;
int fromIndex = 4;
foreach (DataRow dr in dataTable.Rows)
{
IRow row = saveSheet.CreateRow(fromIndex);

ICell cell = row.CreateCell(1);
if (dr["SN"].ToString() != sn)
{
sn = dr["SN"].ToString();
cell.SetCellValue(sn);
lastUserSN = Convert.ToInt32(sn);
}
else
{
lastUserSN = lastUserSN + 10000;
cell.SetCellValue(lastUserSN.ToString());
}
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(2);
cell.SetCellValue("OES/IAM");
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(3);
cell.SetCellValue("China - " + cityName);
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(4);
cell.SetCellValue(dr["PartNumber"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(5);
cell.SetCellValue(dr["PartNumber"].ToString().Replace(" ","").Replace("-",""));
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(6);
cell.SetCellValue(dr["PartDescription_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(7);
cell.SetCellValue(dr["PartDescription_CN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(8);
cell.SetCellValue(dr["VehicleModel_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(9);
cell.SetCellValue(dr["Brand"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(10);
cell.SetCellValue(dr["TypeName_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(11);
cell.SetCellValue(dr["MIID"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(12);
cell.SetCellValue(dr["CorporationName_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(13);
cell.SetCellValue(dr["PartsBrand_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(14);
cell.SetCellValue(dr["QualityLongName_EN"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(15);
cell.SetCellValue(dr["MPMFID"].ToString());
//cell.CellStyle = textWhiteStyle;

//cell = row.CreateCell(16);
//cell.SetCellValue(dr["MPMFID"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(16);
cell.SetCellValue(Convert.ToDateTime(dr["Time"]).ToString("yyyy/MM/dd"));
//cell.CellStyle = textWhiteStyle;

//17列 不知道放什么 R

//18列 固定字符
cell = row.CreateCell(18);
cell.SetCellValue("CNY");
//cell.CellStyle = textWhiteStyle;

//19列 不知道放什么

cell = row.CreateCell(20);
cell.SetCellValue(dr["MSRPWZVAT"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(21);
cell.SetCellValue(dr["DNPWZVAT"].ToString());
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(22);
cell.CellFormula = String.Format("U{0}/R{1}", fromIndex + 1, fromIndex + 1);
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(23);
cell.CellFormula = String.Format("V{0}/R{1}", fromIndex + 1, fromIndex + 1);
//cell.CellStyle = textWhiteStyle;

cell = row.CreateCell(24);
cell.SetCellValue(dr["Remark"].ToString());
//cell.CellStyle = textWhiteStyle;

fromIndex++;
}
}

iworkbook.RemoveSheetAt(1);

MemoryStream memFile = new MemoryStream();
iworkbook.Write(memFile);

FileStream file1 = new FileStream(@"C:/123.xls", FileMode.Create);
iworkbook.Write(file1);
file1.Close();

//C1.C1Excel.C1XLBook book = new C1.C1Excel.C1XLBook();
//book.Load(@"C:/123.xls", C1.C1Excel.FileFormat.OpaqueBiff8, true);
//book.Sheets[0].GetCell(0, 0).Style.BorderColorBottom = Color.Black;
//book.Save(@"C:/123.xls", C1.C1Excel.FileFormat.OpaqueBiff8);


return memFile;
}

private static ICellStyle getWhiteText(HSSFWorkbook hwb)
{
IFont font1 = hwb.CreateFont();
font1.FontHeightInPoints = 9;
font1.FontName = "Arial Narrow";

ICellStyle style2 = hwb.CreateCellStyle();
style2.SetFont(font1);
style2.BorderBottom = BorderStyle.Thin;
style2.BorderLeft = BorderStyle.Thin;
style2.BorderRight = BorderStyle.Thin;
style2.BorderTop = BorderStyle.Thin;
style2.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.Alignment = HorizontalAlignment.Left;
style2.WrapText = true;

return style2;
}
```
I found that if it is set style by the NOPI DLL, the template Excel style will be lost. If I do not implement the "getWhiteText" function, it will not be lost.

Please help me! thanks!
Comments: ** Comment from web user: 87384094 **

怎么没有人帮我解决这个问题啊! 有中国人没?


Created Unassigned: 执行XSSFWorkbook.CreateSheet函数, 创建的Excel有错误 [13154]

$
0
0
首先, 在硬盘上创建一个Excel,任意取名, 然后调用以下函数, 该函数用户读取Excel,文件,并在该文件上,创建Sheet, 在创建的Sheet上填写内容. 总是发生异常

```
public static MemoryStream Export(string tempFilePath)
{
IWorkbook iworkbook = null;
XSSFWorkbook hwb = null;
using (FileStream file = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
{
hwb = new XSSFWorkbook(file);
iworkbook = hwb;
}

ICellStyle textWhiteStyle = getWhiteText(hwb);
for (int i = 0; i < 5; i++)
{
ISheet saveSheet = hwb.CreateSheet(i.ToString());
int lastUserSN = 0;
int fromIndex = 4;

for (int m = 0; m < 100; m++)
{
IRow row = saveSheet.CreateRow(m);

ICell cell = row.CreateCell(1);
cell.SetCellValue(m);
cell.CellStyle = textWhiteStyle;

}
}

MemoryStream memFile = new MemoryStream();
iworkbook.Write(memFile);

FileStream file1 = new FileStream(@"C:/123.xlsx", FileMode.Create);
iworkbook.Write(file1);
file1.Close();

return memFile;
}

private static ICellStyle getWhiteText(XSSFWorkbook hwb)
{
IFont font1 = hwb.CreateFont();
font1.FontHeightInPoints = 9;
font1.FontName = "Arial Narrow";

ICellStyle style2 = hwb.CreateCellStyle();
style2.SetFont(font1);
style2.BorderBottom = BorderStyle.Thin;
style2.BorderLeft = BorderStyle.Thin;
style2.BorderRight = BorderStyle.Thin;
style2.BorderTop = BorderStyle.Thin;
style2.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style2.Alignment = HorizontalAlignment.Left;
style2.WrapText = true;

return style2;
}
```

Created Unassigned: Copy one sheet from one workbook Microsoft Excel to another workbook Microsoft Excel [13155]

$
0
0
Copy one sheet from one workbook Microsoft Excel to another workbook Microsoft Excel (using NPOI Library)

In COM Excel:
```
Excel.Application xlApp=null;
Excel.Workbook xlWbSource ,xlWbTarget;
try { xlApp = new Excel.Application();
xlWbSource = xlApp.Workbooks.Open(@"D:\\table_2014.xls");
xlWbTarget = xlApp.Workbooks.Add();

(xlWbSource.Worksheets[1] as Excel.Worksheet).Copy(Before: xlWbTarget.Worksheets[1]);
xlApp.Visible = true;
}
catch (Exception) {
if (xlApp != null)
xlApp.Quit();
}
```
And how to use this library do not know. Help me please

New Post: Copy one sheet from one workbook Microsoft Excel to another workbook Microsoft Excel C#

$
0
0
Copy one sheet from one workbook Microsoft Excel to another workbook Microsoft Excel (using NPOI Library)

In COM Excel:

Excel.Application xlApp=null;
Excel.Workbook xlWbSource ,xlWbTarget;
try {    xlApp = new Excel.Application();
    xlWbSource = xlApp.Workbooks.Open(@"D:\\table_2014.xls");
    xlWbTarget = xlApp.Workbooks.Add();
    
    (xlWbSource.Worksheets[1] as Excel.Worksheet).Copy(Before: xlWbTarget.Worksheets[1]);
    xlApp.Visible = true;
}
catch (Exception) {
    if (xlApp != null)
        xlApp.Quit();
}
And how to use this library do not know. Help me please

Created Unassigned: rank()的第二个参数不支持单元格多选 [13158]

$
0
0
测试版本NPOI 2.1.1

测试函数:
=RANK(1,($A2,$B3),0)

Commented Unassigned: rank()的第二个参数不支持单元格多选 [13158]

$
0
0
测试版本NPOI 2.1.1

测试函数:
=RANK(1,($A2,$B3),0)
Comments: ** Comment from web user: yyjdelete **

BTW: nuget上的版本没有更新??

Created Unassigned: Buffer overrun while writing a workbook [13159]

$
0
0
Hi,

I'm trying to write an excel using filestream
```
using (FileStream file = new FileStream(selectDest.SelectedPath + @"\Import update LHT.xls", FileMode.Create, FileAccess.Write))
{
wbImport.Write(file);
}
```
and I'm getting this error
```
An unhandled exception of type 'NPOI.Util.RuntimeException' occurred in NPOI.dll

Additional information: Buffer overrun i=111;endIndex=152;writeIndex=43
```

The workbook has only one sheet with about 500 rows so I don't think the size is the problem.

The workbook writing worked until last week and since I keep getting this error and I don't know why.

Created Unassigned: Export progress indication [13162]

$
0
0
Hello,

Is there possible to know the export progress state, in order to display a progress bar for time consuming excel export operations (large files combined with AutoFit) ?
And also is it possible to Cancel a started Excel export operation?

Currently I don't find any other way then displaying a marquee progress bar w/o a Cancel button.

Thanks,
Bogdan

Closed Unassigned: xlsx format file with xls extension issue [13153]

$
0
0
hi i have an xslx format file but the user will upload it with an . xls extension.

IN my code im checking the extension...
if its .xlsx im using XssfWorkboox if its .xls im using HssfWorkbook...
BUt its giving me an error .. "Invalid header".. pls help me with a solution...
user cannot change the extension...


if (fileExt.ToLower() == ".xls")
{
isXlsxXSSFfFile = false;
//Use the NPOI Excel xls object
using (FileStream file = new FileStream(_pathAndFile, FileMode.Open, FileAccess.Read))
{
hssfwb = new HSSFWorkbook(file);
}
if (string.IsNullOrEmpty(_sheetName))
{
hssfSheet = (HSSFSheet)hssfwb.GetSheetAt(0);
}
else
{
if (this.SheetExists(_sheetName))
hssfSheet = (HSSFSheet)hssfwb.GetSheet(_sheetName);
else
throw new ObjectNotFoundException("Sheet name doesn't exist");
}
}
else //.xlsx extension
{
isXlsxXSSFfFile = true;

//Use the NPOI Excel xlsx object
using (FileStream file = new FileStream(_pathAndFile, FileMode.Open, FileAccess.Read))
{
xssfwb = new XSSFWorkbook(file);
}
if (string.IsNullOrEmpty(_sheetName))
{
xssfSheet = (XSSFSheet)xssfwb.GetSheetAt(0);

}
else
{
if (this.SheetExists(_sheetName))
xssfSheet = (XSSFSheet)xssfwb.GetSheet(_sheetName);
else
throw new ObjectNotFoundException("Sheet name doesn't exist");
}

}

Reviewed: NPOI 2.1.1 (7月 28, 2014)

$
0
0
Rated 5 Stars (out of 5) - 123dwqdwqwdqwdwq

Created Unassigned: [Bug] Broken named ranges after existing file modification [13165]

$
0
0
I tried to open existing xlsx file (from excel 2007) and alter named ranges (can be specified in excel in Formulas/Name manager).

Problem is that when file is saved, all names' scope is changed to Sheet instead of whole Workbook. It breaks existing expressions.

Primary issue seems to be in CT_DefinedName class, Parse method is setting fields
ctObj.localSheetId = XmlHelper.ReadUInt(node.Attributes["localSheetId"]);
ctObj.localSheetIdFieldSpecified = node.Attributes["localSheetId"] != null;

while Write method always set localSheedId even if it was not originally present - it causes that field is set to 0 and scope is set to 1st sheet instead of workbook. There should be some check if localSheetIdFieldSpecified is set.
XmlHelper.WriteAttribute(sw, "localSheetId", this.localSheetId, true);


Btw, another bug is that expression "=SHEET_NAME!defined_name" cannot be parsed by NPOI, it fails with error that reference is expected. Expression "=defined_name" works but I cannot use it as it works with global scope names only.

Edited Issue: [Bug] Broken named ranges after existing file modification [13165]

$
0
0
I tried to open existing xlsx file (from excel 2007) and alter named ranges (can be specified in excel in Formulas/Name manager).

Problem is that when file is saved, all names' scope is changed to Sheet instead of whole Workbook. It breaks existing expressions.

Primary issue seems to be in CT_DefinedName class, Parse method is setting fields
ctObj.localSheetId = XmlHelper.ReadUInt(node.Attributes["localSheetId"]);
ctObj.localSheetIdFieldSpecified = node.Attributes["localSheetId"] != null;

while Write method always set localSheedId even if it was not originally present - it causes that field is set to 0 and scope is set to 1st sheet instead of workbook. There should be some check if localSheetIdFieldSpecified is set.
XmlHelper.WriteAttribute(sw, "localSheetId", this.localSheetId, true);


Btw, another bug is that expression "=SHEET_NAME!defined_name" cannot be parsed by NPOI, it fails with error that reference is expected. Expression "=defined_name" works but I cannot use it as it works with global scope names only.

New Post: Initialisation of record 0x31 left 4 bytes remaining still to be read.

$
0
0
在导入某个Excel文件的时候,系统提示报这个错误:Initialisation of record 0x31 left 4 bytes remaining still to be read.
上网百度了下,发现在Java下的版本中也出现了这个问题,但是有牛人解决了这个问题,请问下NPOI中何时可以解决这个问题?(最新版本的NPOI中还是有这个问题)
Viewing all 1621 articles
Browse latest View live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>