Quantcast
Channel: npoi Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 690

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!

Viewing all articles
Browse latest Browse all 690

Trending Articles



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