如果不是绑定数据集的话,在导入的循环中,做个计数,比如 curIndex,每次循环加一,判断curIndex是否在前4或后4,在就不添加
解决方案 »
- c#怎么让窗体永远保持最前
- 求一个合理方便的解决方法
- 如何将Datagridview 的数据 保存至 XML 文件,然后读取?
- 做web开发的时候,如何把个别网络文件映射到本地(浏览器插件?)
- winform问题
- 如何将数据手动写入DataGrid中
- 为何添加新纪录是长文本字段不能够写入ACCESS数据库
- 求一循环语句,非常简单(对我来说比较难)
- asp.net导出excel都有什么方法啊?
- 如何在Visual Studio.net下使用C#编程调用Eecel文件?
- C#一个方法传入一个算术参数,如何计算这个算术题并返回一个Int值
- 控制台程序里面,Timer.BeginInit()/EndInit()会有什么作用吗?
{
if (i <5 ||i>dgvAdd.Columns.Count-4)
{
dgvAdd.Columns[i].visable= false;
}
else
{
dgvAdd.Columns[i].visable= true;
}
}方法就是这样 里面的属性你自己改
读的时候循环数行数总会的吧
我看来下你至直接fill到DT中的
那就等你读进到Datable中的时候在把前4行后4行去掉
读的时候循环数行数总会的吧
我看来下你至直接fill到DT中的
那就等你读进到Datable中的时候在把前4行后4行去掉循环会 但是这个删除怎么删啊
读的时候循环数行数总会的吧
我看来下你至直接fill到DT中的
那就等你读进到Datable中的时候在把前4行后4行去掉循环会 但是这个删除怎么删啊删除什么 ?
读的时候循环数行数总会的吧
我看来下你至直接fill到DT中的
那就等你读进到Datable中的时候在把前4行后4行去掉循环会 但是这个删除怎么删啊删除什么 ?就是删除前四行和后四行 都忘记了 好久没动
读的时候循环数行数总会的吧
我看来下你至直接fill到DT中的
那就等你读进到Datable中的时候在把前4行后4行去掉循环会 但是这个删除怎么删啊删除什么 ?就是删除前四行和后四行 都忘记了 好久没动要么按照主键删除前后4行 要么在从dt1读取到dt2的时候 这4行不要读进去到新的dt中啊这要按实际情况来啊 哥们都这样了 如果你还还写不出那我就没治了
读的时候循环数行数总会的吧
我看来下你至直接fill到DT中的
那就等你读进到Datable中的时候在把前4行后4行去掉循环会 但是这个删除怎么删啊删除什么 ?就是删除前四行和后四行 都忘记了 好久没动要么按照主键删除前后4行 要么在从dt1读取到dt2的时候 这4行不要读进去到新的dt中啊这要按实际情况来啊 哥们都这样了 如果你还还写不出那我就没治了关键就是没用数据库 没有主键啊
/// <summary>
/// DataTable导出到Excel文件
/// </summary>
/// <param name="dtSource">源DataTable</param>
/// <param name="strHeaderText">表头文本</param>
/// <param name="strFileName">保存位置</param>
/// <param name="strSheetName">工作表名称</param>
public static void Export(DataTable dtSource, string strHeaderText, string strFileName, string strSheetName, string[] oldColumnNames, string[] newColumnNames, out bool isSource, out string message)
{
try
{
if (strSheetName == "")
{
strSheetName = "Sheet";
}
using (MemoryStream ms = Export(dtSource, strHeaderText, strSheetName, oldColumnNames, newColumnNames))
{
using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
}
}
message = "导出成功!"; isSource = true;
}
catch (Exception ex)
{
message = ex.Message;
isSource = false;
}
}
/// <summary>
/// DataTable导出到Excel的MemoryStream (没有添加图片)
/// </summary>
/// <param name="dtSource">源DataTable</param>
/// <param name="strHeaderText">表头文本</param>
/// <param name="strSheetName">工作表名称</param>
public static MemoryStream Export(DataTable dtSource, string strHeaderText, string strSheetName, string[] oldColumnNames, string[] newColumnNames)
{
if (oldColumnNames.Length != newColumnNames.Length)
{
return new MemoryStream();
}
HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(strSheetName);
sheet.SetZoom(1, 1);
sheet.DisplayGridlines = false; HSSFSheet print = workbook.GetSheet(strSheetName) as HSSFSheet;
print.PrintSetup.Scale = 80;
print.PrintSetup.Landscape = true;
print.PrintSetup.FitWidth = new short();
print.PrintSetup.FitHeight = new short();
print.PrintSetup.PaperSize = new short();
ICellStyle dateStyle = workbook.CreateCellStyle();
IDataFormat format = workbook.CreateDataFormat();
dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); #region 取得列宽
int[] arrColWidth = new int[oldColumnNames.Length];
for (int i = 0; i < oldColumnNames.Length; i++)
{
arrColWidth[i] = Encoding.GetEncoding(936).GetBytes(newColumnNames[i]).Length;
}
for (int i = 0; i < dtSource.Rows.Count; i++)
{
for (int j = 0; j < oldColumnNames.Length; j++)
{
int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][oldColumnNames[j]].ToString()).Length;
if (intTemp > arrColWidth[j])
{
arrColWidth[j] = intTemp + 10;
}
} }
#endregion
int rowIndex = 0;
//http://www.jb51.net/article/36002.htm
foreach (DataRow row in dtSource.Rows)
{
#region 新建表,填充表头,填充列头,样式
if (rowIndex == 65535 || rowIndex == 0)
{
if (rowIndex != 0)
{
sheet = workbook.CreateSheet(strSheetName + ((int)rowIndex / 65535).ToString());
} #region 表头及样式
{
IRow headerRow = sheet.CreateRow(0);
headerRow.HeightInPoints = 20;
headerRow.CreateCell(0).SetCellValue(strHeaderText); ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
IFont font = workbook.CreateFont();
font.FontHeightInPoints = 15;
font.Boldweight = 750;
headStyle.SetFont(font); headerRow.GetCell(0).CellStyle = headStyle;
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1));
}
#endregion
#region 列头及样式
{
IRow headerRow = sheet.CreateRow(1);
headerRow.HeightInPoints = 25;
ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
IFont font = workbook.CreateFont();
headStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
headStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
headStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
headStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
font.FontHeightInPoints = 12;
font.Boldweight = 700;
headStyle.SetFont(font); for (int i = 0; i < oldColumnNames.Length; i++)
{
headerRow.CreateCell(i).SetCellValue(newColumnNames[i]);
headerRow.GetCell(i).CellStyle = headStyle;
//设置列宽
sheet.SetColumnWidth(i, (arrColWidth[i] + 1) * 256);
}
}
#endregion rowIndex = 2;
}
#endregion #region 填充内容
IRow dataRow = sheet.CreateRow(rowIndex);
dataRow.HeightInPoints = 18;
ICellStyle rowStyle = workbook.CreateCellStyle();
rowStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
rowStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
rowStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
rowStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN; for (int i = 0; i < oldColumnNames.Length; i++)
{
ICell newCell = dataRow.CreateCell(i); string drValue = row[oldColumnNames[i]].ToString(); switch (dtSource.Columns[oldColumnNames[i]].DataType.ToString())
{
case "System.String"://字符串类型
newCell.SetCellValue(drValue);
break;
case "System.DateTime"://日期类型
DateTime dateV;
DateTime.TryParse(drValue, out dateV);
newCell.SetCellValue(dateV); newCell.CellStyle = dateStyle;//格式化显示
break;
case "System.Boolean"://布尔型
bool boolV = false;
bool.TryParse(drValue, out boolV);
newCell.SetCellValue(boolV);
break;
case "System.Int16"://整型
case "System.Int32":
case "System.Int64":
case "System.Byte":
int intV = 0;
int.TryParse(drValue, out intV);
newCell.SetCellValue(intV);
break;
case "System.Decimal"://浮点型
case "System.Double":
double doubV = 0;
double.TryParse(drValue, out doubV);
newCell.SetCellValue(doubV);
break;
case "System.DBNull"://空值处理
newCell.SetCellValue("");
break;
default:
newCell.SetCellValue("");
break;
}
newCell.CellStyle = rowStyle;
}
#endregion rowIndex++;
} #region 新建一行写入总计
{
if (dtSource.Columns.Contains("小计"))
{
IRow headerRow = sheet.CreateRow(rowIndex);
headerRow.HeightInPoints = 15;
ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
IFont font = workbook.CreateFont();
font.FontHeightInPoints = 10;
font.Boldweight = 700;
headStyle.SetFont(font);
headerRow.CreateCell(dtSource.Columns.Count - 6).SetCellValue("CNY合计:");
headerRow.CreateCell(dtSource.Columns.Count - 5).SetCellValue(Convert.ToDouble(dtSource.Compute("SUM(小计)", "小计 is not null and (币制 = 'CNY' or 币制 = 142)")));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, dtSource.Columns.Count - 5, dtSource.Columns.Count - 4));
headerRow.CreateCell(dtSource.Columns.Count - 3).SetCellValue("USD合计:");
if (dtSource.Compute("SUM(小计)", "小计 is not null and (币制 = 'USD' or 币制 = 502)") != DBNull.Value)
headerRow.CreateCell(dtSource.Columns.Count - 2).SetCellValue(Convert.ToDouble(dtSource.Compute("SUM(小计)", "小计 is not null and (币制 = 'USD' or 币制 = 502)")));
headerRow.GetCell(dtSource.Columns.Count - 6).CellStyle = headStyle;
headerRow.GetCell(dtSource.Columns.Count - 5).CellStyle = headStyle;
headerRow.GetCell(dtSource.Columns.Count - 3).CellStyle = headStyle;
if (dtSource.Compute("SUM(小计)", "小计 is not null and (币制 = 'USD' or 币制 = 502)") != DBNull.Value)
headerRow.GetCell(dtSource.Columns.Count - 2).CellStyle = headStyle;
}
}
#endregion using (MemoryStream ms = new MemoryStream())
{
workbook.Write(ms);
ms.Flush();
ms.Position = 0; //sheet.Dispose();
sheet = null;
workbook = null;
//workbook.Dispose();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet
return ms;
}
}